搜索了一下,QTableView中嵌入复选框CheckBox方法有四种:
第一种方法是:编辑委托法
这种方法直接利用委托中重载createEditor(),激活QCheckBox,这个缺点是必须双击/选中,才能显示CheckBox控件。一般不满足我们实际中的直接显示的需要。可以参考Qt中的QSpinBoxDelegate例子。
第二种方法是:设置QAbstractTableModel的flags()函数法。
第三种方法是:用QTableView中的方法void setIndexWidget(constQModelIndex &index, QWidget*widget)来设置QCheckBox。
代码:setIndexWidget(index,new QTextEdit);
If index is invalid (e.g., if you pass the root index), thisfunction will do nothing.
The given widget's autoFillBackground property must be set to true,otherwise the widget's background will be transparent, showing boththe model data and the item at the given index.
If index widget A is replaced with index widget B, index widget Awill be deleted. For example, in the code snippet below, theQLineEdit object will be deleted.
setIndexWidget(index, new QLineEdit);
...
setIndexWidget(index, new QTextEdit);
This function should only be used to display static content withinthe visible area corresponding to an item of data. If you want todisplay custom dynamic content or implement a custom editor widget,subclass QItemDelegate instead.
此功能只应该用来显示可视区域内对应一个数据项的静态内容。如果你想显示自定义的动态内容或执行自定义编辑器部件,子类化QItemDelegate代替。
就是说这个只适合用来做不变数据的显示,而不适合做一些会产生插入,更新,删除这样的操作的数据显示.
本文介绍了在Qt框架下QTableView中嵌入复选框的四种方法:编辑委托法、设置flags函数法、使用setIndexWidget方法及实现QAbstractItemDelegate的paint函数。这些方法分别适用于不同场景,如直接显示、静态数据展示、自定义风格等。
1703

被折叠的 条评论
为什么被折叠?



