转载:QTableView中嵌入可视化组件
出处: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 写道关于setIndexWidget()
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中嵌入可视化组件的更多相关文章
- QTableView中嵌入复选框CheckBox 的四种方法总结
搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四 ...
- 在Flutter中嵌入Native组件的正确姿势是...
引言 在漫长的从Native向Flutter过渡的混合工程时期,要想平滑地过渡,在Flutter中使用Native中较为完善的控件会是一个很好的选择.本文希望向大家介绍AndroidView的使用方式 ...
- qt QTableView中嵌入复选框CheckBox 的四种方法总结
第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件 ...
- 在Vue中echarts可视化组件的使用
echarts组件官网地址:https://echarts.apache.org/examples/zh/index.html 1.找到脚手架项目所在地址,执行cnpm install echarts ...
- python QQTableView中嵌入复选框CheckBox四种方法
搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四 ...
- 在WPF中嵌入WebBrowser可视化页面
无论是哪种C/S技术,涉及数据可视化就非常的累赘了,当然大神也一定有,只不过面向大多数人,还是通过网页来实现,有的时候不想把这两个功能分开,一般会是客户的原因,所以我们打算在WPF中嵌入WebBrow ...
- VC中调用COM组件的方法(转载)
原文参考:http://hi.baidu.com/mingyueye/item/53ebecd44da76917d80e4449 总结一下在VC中调用COM组件的方法 准备及条件: COM服务器为进程 ...
- 工程日记之HelloSlide(1):Swift自定义可视化组件的方法(继承UIView和在StoryBoard中设置)
需求描述 HelloSlide是把文本自动转化成幻灯片的软件,在幻灯片中我们有SmartArt:各种各样的几何形状,并且可以自定义大小和颜色,放在幻灯片不同的位置. 为了在我们的软件中实现类似的效果, ...
- 转-使用 CefSharp 在 C# App 中嵌入 Chrome 浏览器
使用 CefSharp 在 C# App 中嵌入 Chrome 浏览器 2016-09-23 分类:.NET开发.编程开发.首页精华0人评论 分享到:更多3 本文由码农网 – 小峰原创翻译,转载 ...
随机推荐
- 【转】python2与python3的主要区别
摘自:http://www.cnblogs.com/codingmylife/archive/2010/06/06/1752807.html 1.性能 Py3.0运行 pystone benchmar ...
- linux分享一:进程全攻略--守护进程(服务)
概括: 进程是程序的运行实例.进程对应一个唯一的进程PID, 统一程序的多个实例可以同时运行,他们的pid互不相同. 进程一般分为交互进程.批处理进程和守护进程(daemons)三类 一:什么是守护进 ...
- 关于spring xml文件中的xmlns,xsi:schemaLocation(转)
使用spring也有一段时间了,配置文件也见了不少了,但是发现配置文件的beans里面有很多链接,一开始也很迷惑,所以抽了一点时间整里了一下. 首先我们看到的一个spring的配置文件大概如下面这个样 ...
- 豌豆荚不能连接三星S4手机,提示打开手机的“USB调试模式”,但却找不到在哪儿可以设置
1. 问题 豌豆荚不能连接三星S4手机,下载了三星S4手机的驱动后,提示打开手机的“USB调试模式”,但却找不到在哪儿可以设置. 2. 原因 S4 默认隐藏开发者选项,所以默认不可以设置“USB调试模 ...
- jquery动态加载js三种方法实例
这里为你提供了三种动态加载js的jquery实例代码哦,由于jquery是为用户提供方便的,所以利用jquery动态加载文件只要一句话$.getScript(\"test.js\" ...
- 每日英语:Why Are Fractions Key To Future Math Success?
Many students cruise along just fine in math until fourth grade or so. Then, they hit a wall -- frac ...
- Oracle PLSQL Demo - 18.01管道function[查询零散的字段组成list管道返回]
--PACKAGE CREATE OR REPLACE PACKAGE test_141213 is TYPE type_ref IS record( ENAME ), WORK_CITY ), SA ...
- 10、Windows10 上,在窗口左侧向右滑动打开 SplitView 的 Pane面板
昨天想在 uwp 上实现,在 SplitView 控件的左侧,通过手指滑动打开 SplitView 的 Pane 面板, 而不仅仅是通过 “汉堡按钮” 点击打开. 在 stackoverflow 看到 ...
- 如何在Windows环境搭建Object C开发环境
1. 安装编译环境 Object C和其他很多语言一样,都需要有一个编译器.Object C 是在GCC下编译的.GCC(GNU Compiler Collection,GNU编译器集合),是一套由 ...
- 【Maven学习】Maven打包生成包含所有依赖的jar包
http://blog.csdn.net/u013177446/article/details/54134583 ******************************************* ...