zoukankan      html  css  js  c++  java
  • QTableView 二次整理

    一、设置可视化的组件

    参考:

      http://www.cnblogs.com/ribavnu/p/4810412.html

    二、常用基本属性

      http://www.cnblogs.com/ribavnu/p/4791393.html

    三、编辑后提示是否保存

      http://www.cnblogs.com/ribavnu/p/4702532.html

    四、设置特定列是否能编辑

    #
    #   C_TableView
    #  在QTreeView基础上增加限制 特定列是否 可以编辑
    #
    class C_TableView(QTableView): 
        def __init__(self, parent=None):
            QTableView.__init__(self, parent)
            # 设置可以编辑的   列
            # column:list
            self.c_allow_edit = list()
        #
        # 设置可以编辑的    列
        # allow_edit :list,
        # value : 可以编辑的多个列, 列编号从0开始
        def c_setAllowEdit(self, allow_edit):
            self.c_allow_edit = allow_edit
            
        def edit(self, index, trigger, event):
        
            args = [index, trigger, event]        
            columnIndex = index.column()
            
            if event is not None:
                # 2:鼠标的按下,3:鼠标的释放
                # 4:鼠标的双击
                # 如果  列  是否可以编辑
                event_type = event.type()
                if event_type ==4:
                    if columnIndex not in self.c_allow_edit :
                        return False
                        
            return_value =super(QTableView, self) .edit( *args) 
            return return_value                                                   
               
    

    五、设置特定列的代理,对 一 的 补充。

    void QAbstractItemView::setItemDelegateForRow(int row, QAbstractItemDelegate *delegate)

    # 自定义复选框checkbox
    class Delegate_CheckBox(QtWidgets.QStyledItemDelegate):
        
        def createEditor(self, parent, option, index):
            
                editor = QtWidgets.QCheckBox('是', parent)
                # 填充背景
                #editor.setAutoFillBackground(True) 
                #editor.setGeometry(self.checkBoxRect(option))
                
                return editor
            
        # 计算 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):
               
                    from PyQt5.QtWidgets import  QStyle
                    
                    # 如果选中则高亮
                    if option.state & QStyle.State_Selected:
                            painter.fillRect(option.rect, option.palette.highlight())
               
                    #获取值  
                    checked = index.model().data(index, QtCore.Qt.DisplayRole)
                    checked = int(checked)
                    #按钮的风格选项  
                    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)  
                    
        def setEditorData(self, spinBox, index):
            
                data = index.data()
                data = int(data)
                if data  > 0:
                    spinBox.setChecked(True)
                else:
                    spinBox.setChecked(False)
                
           
        def setModelData(self, spinBox, model, index): 
            
                data = spinBox.isChecked()
                if data:
                        model.setData(index, 1)
                else:
                        model.setData(index, 0)
           
            
        def updateEditorGeometry(self, editor, option, index):
            # 原始的就这么一句话
            #editor.setGeometry(option.rect)
            
            # 居中显示
            rect = option.rect
            option_x= rect.x()
            option_y = rect.y()
            option_width = rect.width()
         # 微调下左边,误差原因不明 rect.moveTo(option_x + option_width /2 - 8 , option_y) editor.setGeometry(rect)

     六、

    设置tableview背景透明

                    from PyQt5.QtGui import QPalette, QBrush, QColor
                    palette  = tableView.palette()
                    palette.setBrush(QPalette.Base,QBrush(QColor(255,255,255,0)))
                    tableView.setPalette(palette)        
    

      

      

  • 相关阅读:
    要搜索内容
    .net core 过滤器
    C# => 写法
    js 数组的forEach 函数
    .net core 下载文件 其他格式
    win10 1903 更改文字大小
    fetch 写法
    C# 匿名对象 增加属性
    ping —— 虚拟机
    selenium验证车贷计算器算法
  • 原文地址:https://www.cnblogs.com/ribavnu/p/5012141.html
Copyright © 2011-2022 走看看