PyQt4 QTableView单元格文本在行选择

时间:2018-04-23 17:39:06

标签: python pyqt pyqt4 qtableview

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

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

enter image description here

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

enter image description here

在这种情况下,有没有办法让标记为红色的细胞保持这种状态?

我正在为每个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。 现在文本保持其颜色,但没有突出显示,我不知道如何保持行的浅红色。

enter image description here

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)

有没有办法定位这些特定的细胞并保持行突出显示?

1 个答案:

答案 0 :(得分:0)

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

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