QTableView 二次整理

简介: 一、设置可视化的组件 参考:   http://www.cnblogs.com/ribavnu/p/4810412.html   二、常用基本属性   http://www.cnblogs.com/ribavnu/p/4791393.

一、设置可视化的组件

参考:

  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)        

  

 

 

 

  

相关文章
|
4月前
|
程序员 C++ 计算机视觉
|
4月前
CSDN博客置顶操作
CSDN博客置顶操作
18 0
|
iOS开发
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
268 0
|
数据安全/隐私保护
CSDN写博客之图片居中、去水印、改大小及文字居中
CSDN写博客之图片居中、去水印、改大小及文字居中
CSDN写博客之图片居中、去水印、改大小及文字居中
|
前端开发
前端百题斩【020】——竟然有五种方式实现flat方法
前端百题斩【020】——竟然有五种方式实现flat方法
csdn写文章如何修改字体颜色
csdn写文章如何修改字体颜色
125 0
|
前端开发
前端进阶|第十三天 opacity: 0,visibility: hidden,display: none看不见元素看得见的属性
前端进阶|第十三天 opacity: 0,visibility: hidden,display: none看不见元素看得见的属性,每天一个知识点。
2630 0

热门文章

最新文章