繁体   English   中英

PyQt4 QTableView单元格文本在行选择时更改颜色

[英]PyQt4 QTableView cell text changes color on row selection

我正在开发一个使用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)

有什么办法针对那些特定的单元格并使行突出显示吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM