【问题标题】:PyQt4 QTableView cell text changes color on row selectionPyQt4 QTableView 单元格文本在行选择时更改颜色
【发布时间】:2018-10-03 20:18:49
【问题描述】:

我正在开发一个使用 PyQt4 比较 *.xls 和 *.xlsx 文件的小型应用程序。

这是比较窗口的样子。行以浅红色突出显示,而单元格文本以鲜红色突出显示。

当我通过单击选择行时,所有红色突出显示都消失了。

在这种情况下,有什么方法可以让标记为红色的单元格保持这种状态?

我为每个 QtGui.QTableView 使用 QtCore.QAbstractTableModel

def data(self, index, role):

    """
    Updating tableView data

    """

    #########################################################
    #            Data role -> Updating cell contents        #  
    #########################################################  

    if role == QtCore.Qt.DisplayRole:
        row = index.row()
        if 0 <= row < self.rowCount():
            column = index.column()
            if 0 <= column < self.columnCount():
                return self._data[row][column]

    #########################################################
    #          Background role -> Updating row color        #  
    ######################################################### 

    if role ==  QtCore.Qt.BackgroundRole and index.row() in self._marked_rows:
        customColor = QtGui.QColor(255, 204, 204)
        return QtCore.QVariant(QtGui.QBrush(customColor))  

    #########################################################
    #     TextColorRole role -> Updating cell text color    #
    ######################################################### 

    if role == QtCore.Qt.ForegroundRole:
        if str(index.row()) + ':' + str(index.column()) in self._marked_cells:
            return QtCore.QVariant(QtGui.QColor(QtCore.Qt.red))

经过一些搜索后,我尝试使用 QtGui.QStyledItemDelegate。 现在文本保持其颜色,但没有突出显示,我不知道如何保持该行的浅红色。

class TextColorDelegate(QtGui.QStyledItemDelegate):

    def __init__(self, cells, parent = None):

        QtGui.QStyledItemDelegate.__init__(self, parent)
        self.cells = cells

    def paint(self, painter, option, index):

       painter.save()

       value = index.data(QtCore.Qt.DisplayRole)
       if value.isValid():
           text = value.toString()

           if str(index.row()) + ':' + str(index.column()) in self.cells:
               painter.setPen(QtGui.QPen(QtCore.Qt.red))
               painter.drawText(option.rect, QtCore.Qt.AlignVCenter, text)
           else:
               painter.setPen(QtGui.QPen(QtCore.Qt.black))
               painter.drawText(option.rect, QtCore.Qt.AlignVCenter, text)

       painter.restore()

self.model_1 = TableModel(_data_1, 
                         columns_string, 
                         _markedRows_1, 
                         _markedCells_1, 
                         self.appPath, 
                         self.tableView_1)

delegate_1 = TextColorDelegate(_markedCells_1, self)
self.tableView_1.setModel(self.model_1)
self.tableView_1.setItemDelegate(delegate_1)

有什么方法可以定位那些特定的单元格并保持该行突出显示?

【问题讨论】:

    标签: python pyqt pyqt4 qtableview


    【解决方案1】:

    我设法自己找到了解决方案。也许更简单的方法是可能的,但它确实有效。

    class TextColorDelegate(QtGui.QStyledItemDelegate):
    
        """
        Delegate used for changing text color and highlighting behaviour  
    
        """ 
    
        def __init__(self, cells, parent = None):
    
            """
            Object initialization
    
            cells - marked cells that have different content
    
            """
    
            QtGui.QStyledItemDelegate.__init__(self, parent)
            self.cells = cells
    
        def paint(self, painter, option, index):
    
            """
            Painter function used for overriding display behaviour
    
            """
    
            painter.save()
    
            displayText    = index.data(QtCore.Qt.DisplayRole)
            backgroudColor = index.data(QtCore.Qt.BackgroundColorRole)
    
            customColor    = QtGui.QColor(255, 204, 204)
            blackColor     = QtCore.Qt.black
            yellowColor    = QtCore.Qt.yellow 
            redColor       = QtCore.Qt.red
    
            backgroundFlag = backgroudColor.isValid()
            textFlag       = displayText.isValid()
            textContent    = displayText.toString()
    
            if backgroudColor.isValid():
    
                ###################################################################
                #               Evaluating rows with differences                  #
                #   Adjusting background and text color depending on QStyleState  #
                ###################################################################
    
                painter.fillRect(option.rect, customColor)   #set row background color 
    
                if (option.state & QtGui.QStyle.State_Selected):
                    painter.fillRect(option.rect, option.palette.highlight())       
                    color_to_set = yellowColor if ( str( index.row() ) + ':' + str( index.column() ) ) in self.cells else blackColor                                              
                else:
                    color_to_set = redColor if ( str( index.row() ) + ':' + str( index.column() ) ) in self.cells else blackColor
    
                ###################################################################
                #               Evaluating rows with no differences               #
                #   Adjusting background and text color depending on QStyleState  #
                ###################################################################
    
            else:
                if (option.state & QtGui.QStyle.State_Selected) : painter.fillRect(option.rect, option.palette.highlight()) 
                color_to_set = blackColor
    
    
            if textFlag:                                                     
                painter.setPen(QtGui.QPen(color_to_set))
                painter.drawText(option.rect, QtCore.Qt.AlignVCenter, textContent)
    
            painter.restore()
    

    现在突出显示工作。

    https://i.imgur.com/drpliS4.jpg
    https://i.imgur.com/RiO0d4I.jpg

    【讨论】:

      猜你喜欢
      • 2017-08-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2011-04-11
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多