简体   繁体   中英

PyQt5 : Change font color in one cell if row is selected (QAbstractTableModel)

I have a QTableView with model and want to change the foreground color in a specific column if i select a row. I tried something out with a QStyledItemDelegate but nothing works. could anybody help me?

The SelectionBehavior of the tableView is SelectRows .

With StyleSheets on the Tableview i set the color to 'color:rgb(255,255,255)' and the selection-color to 'color:rgb(255, 181, 62)' . But if i select a row, the color of column 8 should be stay 'color:rgb(255,255,255)'

thank you very much

edit:

from PyQt5 import QtWidgets
from PyQt5 import QtGui

class reportDelegate(QtWidgets.QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super(reportDelegate, self).initStyleOption(option, index)

        color = QtGui.QColor(255,255,255)

        if index.column() == 8:
            color = QtGui.QColor(255,255,255)
            if option.state & QtWidgets.QStyle.State_Selected:
                option.palette.setColor(QtGui.QPalette.Normal, QtGui.QPalette.HighlightedText, QtGui.QColor(255,255,255))
        else:
            if option.state & QtWidgets.QStyle.State_Selected:
                option.palette.setColor(QtGui.QPalette.Normal, QtGui.QPalette.HighlightedText, QtGui.QColor(255, 181, 62))
        option.palette.setBrush(QtGui.QPalette.Text, color)

Now i got it. It works. But i have another problem. When i doubleclick on a row, a QDialog opens. If the QDialog is open, the color of the selected row switched to black, but it should stay white. I dont know the state of the selected, but inactive or whatelse row, so i dont know how the make the row white in this state.

edit: Now it works fine. I deactivated the color in the meantime when I tried something. Now i activated it again and the row stays white.

If someone has a better solution for all the things above, I would be very grateful.

The foreground ("text") color used for selected items is set using the HighlightedText ColorRole .

When you also specify the ColorGroup , the color set will only be valid for that selected group, while reverting to the parent (or system) palette if the inherited role isn't set.

If the view becomes inactive (aka: unfocused), and you explicitly set the color for another color group ( Normal , in your case), Qt will use the default color for any other role and/or group.

You need to set the palette color for the role and all color groups, so just omit the color group at all:

class reportDelegate(QtWidgets.QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super().initStyleOption(option, index)

        if index.column() == 8:
            highlightColor = QtGui.QColor(255, 255, 255)
        else:
            highlightColor = QtGui.QColor(255, 181, 62)

        option.palette.setColor(QtGui.QPalette.HighlightedText,
            highlightColor)

Notes:

  • QPalette always stores colors as QBrush objects, even when using QColor or Qt.GlobalColor arguments (it internally creates QBrush objects anyway);
  • using setStyleSheet() with the ::item selector and the background property will always override the background, no matter if the Qt.BackgroundRole is set (see this related answer ;
  • the QStyle.State_Selected is usually irrelevant with this kind of checking, as the style will always verify the state anyway; just ignore the state, set the palette color no matter what, and let the default implementation do the rest; this is important as it's the paint() responsibility to finally choose the actual color to use;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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