QTableView中嵌入CheckBox的四种方法

搜索了一下,QTableView中嵌入复选框CheckBox方法有四种

 

 

       第一种不能之前显示,必须双击/选中后才能显示,不适用。

       第二种比较简单,通常用这种方法。

       第三种只适合静态显示静态数据用

      第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件,图片等自定义风格。


第一种方法是:编辑委托法

这种方法直接利用委托中重载createEditor(),激活QCheckBox,这个缺点是必须双击/选中,才能显示CheckBox控件。一般不满足我们实际中的直接显示的需要。可以参考Qt中的QSpinBoxDelegate例子。

 

第二种方法是:设置QAbstractTableModel的flags()函数法。

 

第三种方法是:用QTableView中的方法void setIndexWidget(constQModelIndex &index, QWidget*widget)来设置QCheckBox。

 

代码:setIndexWidget(index,new QTextEdit);

  Qt Assistant 写道
The items shown in a table view, like thosein the other item views, are rendered and edited using standarddelegates. However, for some tasks it is sometimes useful to beable to insert widgets in a table instead. Widgets are set forparticular indexes with the setIndexWidget() function, and laterretrieved with indexWidget().
 

    QtAssistant 写道关于setIndexWidget()

Sets the given widget on the item at thegiven index, passing the ownership of the widget to theviewport.
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代替。

就是说这个只适合用来做不变数据的显示,而不适合做一些会产生插入,更新,删除这样的操作的数据显示.

 

 

第四种方法是:实现QAbstractItemDelegate的paint()函数。

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值