转载: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 本文由码农网 – 小峰原创翻译,转载 ...
随机推荐
- css常用标签及属性
css样式表常用的形式有三种,一.行内样式表.二.内部样式表.三.外部样式表 一. <p style="color:red;">nice to meet you< ...
- 【转】sql server存储过程中SELECT 与 SET 对变量赋值的区别
转自:http://www.cnblogs.com/micheng11/archive/2008/07/08/1237905.html SQL Server 中对已经定义的变量赋值的方式用两种,分别是 ...
- jQuery $.extend()使用方法
$.extend()使用方法总结. jQuery为开发插件提拱了两个方法,各自是: jQuery.fn.extend(object); jQuery.extend(object); jQuery.ex ...
- Python中将字典转换为有序列表、无序列表的方法
说明:列表不可以转换为字典 1.转换后的列表为无序列表 a = {'a' : 1, 'b': 2, 'c' : 3} #字典中的key转换为列表 key_value = list(a.keys()) ...
- 去除img、video之间默认间隔的几种方法
img,video{ /*第1种方式*/ border: ; vertical-align: bottom; /*第2种方式*/ outline-width:0px; vertical-align:t ...
- ClouderaManager之CDH-LZO配置
CDH-LZO配置 下载和CDH版本对应的hadoop-lzo版本 如下: 下载地址:http://archive.cloudera.com/gplextras5/parcels/ 需要下载如下三个文 ...
- js 轮播插件
flexslider pc插件 个人用过 flickerplate 移动端插件 个人用过 个人觉得比较好的移动端插件 swiper http://www.swiper.com.cn/ 用过 个人觉 ...
- poi 抽取execl表面数据源代码工具
开发中 ,导入导出execl避免不了数据类型格式的校验,在使用poi要使用抽取表面数据,poi暂时不支持单元格抽取,查询poi源码抽取工具类如下,如使用jxl就不必使用,jxl取出的单元格数据已是抽取 ...
- linux的一些软件基本安装
买了个腾讯云的服务器,开始玩起来了,先装环境吧. JAVA安装 安装个java yum -y install java-1.7.0-openjdk* 查看java版本 java -version 可以 ...
- 【C#】获取泛型<T>的真实类型
需求:在包含泛型T的类或方法中,想要根据T的具体类型,进行相应的处理,需求伪代码如下: public void Test<T>() { if(T is string) { // do so ...