• 16594阅读
  • 8回复

QTableview内如何将pushbutton嵌入到cell? [复制链接]

上一主题 下一主题
离线arm_du
 

只看楼主 倒序阅读 楼主  发表于: 2008-09-23
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
查看了Qt自带的几个delegate的demo,还是没有解决这个问题。spinbox的例子是要在cell内双击才能显示spinbox,现在是希望tableview初始化后直接在一列单元内显示一列按钮。请高人指点了。谢谢先。
离线robertkun

只看该作者 1楼 发表于: 2009-08-05
顶 ,
                                              简单的生活使人快乐!
离线sentimental
只看该作者 2楼 发表于: 2009-08-05
为什么不用qtablewidget?
离线robertkun

只看该作者 3楼 发表于: 2009-08-06
引用第2楼sentimental于2009-08-05 12:59发表的  :
为什么不用qtablewidget?


不知道QtableWidget 怎样实现的?
                                              简单的生活使人快乐!
离线wxyao

只看该作者 4楼 发表于: 2010-09-29
谁知道呢,我现在也需要这个功能,有人说过QTableWidget::setCellWidget,可是QTableView里面并没有这个。 有更好的吗?
离线wxyao

只看该作者 5楼 发表于: 2010-09-29


使用QTableWidget::setCellWidget 还行。

QTableWidget *tableWidget = new QTableWidget(4, 3);

QPushButton *pushButton_test = new QPushButton("TEST…");
tableWidget ->setCellWidget(2, 2, pushButton_test);

----------------------------------------------------------------------------------------------------------------------------------------------
通过查看源代码,得知QTableWidget::setCellWidget调用了QAbstractItemView::setIndexWidget
  1. //qtablewidget.cpp
  2. void QTableWidget::setCellWidget(int row, int column, QWidget *widget)
  3. {
  4.     QModelIndex index = model()->index(row, column, QModelIndex());
  5.     QAbstractItemView::setIndexWidget(index, widget);
  6. }


那么,就可以直接使用view的QAbstractItemView::setIndexWidget来实现喽~~~

---------------------------------------------------------------------PS-------------------------------------------------------------------------

void QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget )
Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.
If index is invalid (e.g., if you pass the root index), this function will do nothing.
The given widget's autoFillBackground property must be set to true, otherwise the widget's background will be transparent, showing both the model data and the item at the given index.
If index widget A is replaced with index widget B, index widget A will be deleted. For example, in the code snippet below, the QLineEdit object will be deleted.
setIndexWidget(index, new QLineEdit);
...
setIndexWidget(index, new QTextEdit);
This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QItemDelegate instead.
[ 此帖被wxyao在2010-09-29 16:46重新编辑 ]
离线wxyao

只看该作者 6楼 发表于: 2010-09-29
又有问题了...

虽然可以嵌入按钮,但是我点击那个位置的按钮之后,得不到cellClicked(int, int)信号啊,就是说不知道在哪行哪列点击了按钮?
[ 此帖被wxyao在2010-09-29 17:54重新编辑 ]
离线米饭拌面

只看该作者 7楼 发表于: 2010-09-30
这个点击信号你可以通过pushbutton的点击信号来获得
QQ:32336134
离线wxyao

只看该作者 8楼 发表于: 2011-03-04
This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QItemDelegate instead.

这段话的意思好象是说,使用tableWidget ->setCellWidget只能设置静态的用于显示的widget,如果是QCheckBox或QSpinBox就得使用代理QItemDelegate。

是这个意思吧,但是我现在不知道怎样“不用双击触发就显示QCheckBox”
快速回复
限100 字节
 
上一个 下一个