• 转载: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 写道
    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)
    
  • 相关阅读:
    Node.js 函数
    Node.js模块系统
    在Apache服务器上安装SSL证书
    Node.js Stream(流)--文件操作
    HTML5自带验证美化
    HTML5约束验证API
    Node.js Buffer(缓冲区)
    Python 练习:简单的购物车(二)
    Python 练习:简单的购物车
    Python 列表操作
  • 原文地址:https://www.cnblogs.com/ribavnu/p/4810412.html
Copyright © 2020-2023  润新知