出处:http://qimo601.iteye.com/blog/1538364

QTableView中嵌入可视化组件方法有四种

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

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

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

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

 

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

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

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

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

代码:setIndexWidget(index, new QTextEdit);

 Qt Assistant 写道
The items shown in a table view, like those in the other item views, are rendered and edited using standard delegates. However, for some tasks it is sometimes useful to be able to insert widgets in a table instead. Widgets are set for particular indexes with the setIndexWidget() function, and later retrieved with indexWidget().

Qt Assistant 写道关于setIndexWidget()

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.

此功能只应该用来显示可视区域内对应一个数据项的静态内容。如果你想显示自定义的动态内容或执行自定义编辑器部件,子类化QItemDelegate代替。

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

此处向下为 博主 自己 原载:

在tableview中添加 QCheckBox:

class C_Delegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
columnIndex = index.column()
if columnIndex == 5:
editor = QtWidgets.QCheckBox('是', parent)
editor.setAutoFillBackground(True) return editor
else:
return super(C_Delegate, self).createEditor(parent, option, index) # 计算 check_box的位置 和 大小
def checkBoxRect(self, option): but_style = QtWidgets.QStyleOptionButton()
check_box_rect =QtWidgets. QApplication.style().subElementRect(
QtWidgets.QStyle.SE_CheckBoxIndicator,
but_style) check_box_point = QtCore.QPoint(option.rect.x() +
option.rect.width() / 2 -
check_box_rect.width() / 2,
option.rect.y() +
option.rect.height() / 2 -
check_box_rect.height() / 2);
return QtCore.QRect(check_box_point, check_box_rect.size()) def paint(self, painter, option, index):
columnIndex = index.column()
if columnIndex == 5: #获取值
checked = index.model().data(index, QtCore.Qt.DisplayRole)
#按钮的风格选项
checkBoxOption = QtWidgets.QStyleOptionButton()
checkBoxOption.state |= QtWidgets.QStyle.State_Enabled;
#根据值判断是否选中
if checked > 0 :
checkBoxOption.state |= QtWidgets.QStyle.State_On
else :
checkBoxOption.state |= QtWidgets.QStyle.State_Off #返回QCheckBox几何形状
checkBoxOption.rect = self.checkBoxRect(option)
#绘制QCheckBox
QtWidgets.QApplication.style().drawControl(QtWidgets.QStyle.CE_CheckBox,checkBoxOption,painter) else: super(C_Delegate, self).paint(painter, option, index) def setEditorData(self, spinBox, index):
columnIndex = index.column()
if columnIndex == 5:
data = index.data()
if data > 0:
spinBox.setChecked(True)
else:
spinBox.setChecked(False) else:
super(C_Delegate, self).setEditorData( spinBox, index) def setModelData(self, spinBox, model, index): columnIndex = index.column()
if columnIndex == 5:
data = spinBox.isChecked()
if data:
model.setData(index, 1)
else:
model.setData(index, 0)
else:
super(C_Delegate, self).setModelData( spinBox, model, index) def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)

转载:QTableView中嵌入可视化组件的更多相关文章

  1. QTableView中嵌入复选框CheckBox 的四种方法总结

    搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四 ...

  2. 在Flutter中嵌入Native组件的正确姿势是...

    引言 在漫长的从Native向Flutter过渡的混合工程时期,要想平滑地过渡,在Flutter中使用Native中较为完善的控件会是一个很好的选择.本文希望向大家介绍AndroidView的使用方式 ...

  3. qt QTableView中嵌入复选框CheckBox 的四种方法总结

    第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件 ...

  4. 在Vue中echarts可视化组件的使用

    echarts组件官网地址:https://echarts.apache.org/examples/zh/index.html 1.找到脚手架项目所在地址,执行cnpm install echarts ...

  5. python QQTableView中嵌入复选框CheckBox四种方法

    搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四 ...

  6. 在WPF中嵌入WebBrowser可视化页面

    无论是哪种C/S技术,涉及数据可视化就非常的累赘了,当然大神也一定有,只不过面向大多数人,还是通过网页来实现,有的时候不想把这两个功能分开,一般会是客户的原因,所以我们打算在WPF中嵌入WebBrow ...

  7. VC中调用COM组件的方法(转载)

    原文参考:http://hi.baidu.com/mingyueye/item/53ebecd44da76917d80e4449 总结一下在VC中调用COM组件的方法 准备及条件: COM服务器为进程 ...

  8. 工程日记之HelloSlide(1):Swift自定义可视化组件的方法(继承UIView和在StoryBoard中设置)

    需求描述 HelloSlide是把文本自动转化成幻灯片的软件,在幻灯片中我们有SmartArt:各种各样的几何形状,并且可以自定义大小和颜色,放在幻灯片不同的位置. 为了在我们的软件中实现类似的效果, ...

  9. 转-使用 CefSharp 在 C# App 中嵌入 Chrome 浏览器

    使用 CefSharp 在 C# App 中嵌入 Chrome 浏览器 2016-09-23    分类:.NET开发.编程开发.首页精华0人评论 分享到:更多3 本文由码农网 – 小峰原创翻译,转载 ...

随机推荐

  1. 使用latex撰写博士,硕士学位论文(浙大博士经验分享)

    使用latex撰写博士,硕士学位论文(浙大博士经验分享) 浙大博士:  个人感觉,还是要用latex来写.因为之前发过几篇word排版的中文论文,在参考文献的引用.文字格式调整上,实在是难受.如果坚持 ...

  2. 64位win8.1系统安装intelhaxm

    加快安卓模拟器的启动速度,需要装intelhaxm,以前win8时直接双击网上下载的exe文件就安装得了,但是win8.1的时候双击了总提示说是vt-x没有启用的,但是我看任务管理器→性能标签页那里的 ...

  3. AAA含义图解

    来源: <FreeRADIUS Beginner's Guide> 这本书 1,认证 2,授权 3,审计

  4. [sh]sed 4个功能

    [root@lanny test]# cat test.txt test liyao lanny 经典博文: http://oldboy.blog.51cto.com/2561410/949365 h ...

  5. IServerChannelSinkProvider

    (一) Remoting框架图 这是msdn上关于Remoting客户端与服务器端进行通信的示意图.客户端与服务端的通信是通过发送消息来实现的.消息的处理是由客户端,服务端创建的一系列的通信信道来处理 ...

  6. 关于locate这个NB命令我不得不深入的学习

    先看看locate的安装包和生成的文件: [root@NB mlocate]# which locate /usr/bin/locate [root@NB mlocate]# rpm -qf /usr ...

  7. Cocos-2d 坐标系

    Cocos-2d中,涉及到4种坐标系: GL坐标系:Cocos2D以OpenglES为图形库,所以它使用OpenglES坐标系.GL坐标系原点在屏幕左下角,x轴向右,y轴向上. getLocation ...

  8. LeetCode: Combinations 解题报告

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...

  9. java.io.PrintWriter 中 write() 与 print() 的区别

    最终都是重写了抽象类Writer里面的write方法print方法可以将各种类型的数据转换成字符串的形式输出.重载的write方法只能输出字符.字符数组.字符串等与字符相关的数据.

  10. gvim配置到命令行可以使用

    下载安装gvim后,把gvim的安装目录添加到环境变量.