How to keep original text colors, set using setForeground(), when QTableWidgetItem is selected

773 views Asked by At

My Problem:

Right now, as you can see on Fig 2 below, as the first row is selected, all texts' colors are set to black, instead of keeping their original style displayed on Fig 1.

Is there a way to keep the original colors when the row is selected (as shown in Fig 3)? Should I keep relying on CSS alone or should I just create a QAbstractItemDelegate to fix this problem?

If a QAbstractItemDelegate is needed, is it faster than using CSS or does it slow down the application?

enter image description here

Fig 1: How it looks like without any selection.

enter image description here

Fig 2: How it looks like when user selects Row 0.

enter image description here

Fig 3: Made on Paint, how I would like it to behave.


Settings:

I am using PySide2: Qt5 on Python 3.10 on Windows 10.

In my example, I have set a few properties on my QTableWidget before inserting rows inside it:

self.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.setDragDropMode(QAbstractItemView.NoDragDrop)
self.setFocusPolicy(Qt.NoFocus)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.setSelectionMode(QAbstractItemView.SingleSelection)
self.setAlternatingRowColors(True)
self.setShowGrid(False)

tv = self.verticalHeader()
tv.setVisible(False)
tv.setSectionsClickable(False)
tv.setSectionResizeMode(QHeaderView.Fixed)
tv.setDefaultAlignment(Qt.AlignHCenter)

th = self.horizontalHeader()
th.setHighlightSections(False)
th.setStretchLastSection(True)
th.setSectionResizeMode(QHeaderView.Interactive)

# With Exception to Column 0: ResizeMode is ResizeToContents

Then I created a function to easily insert items inside it. It's a big function, which relies on a few methods from QTableWidget and QTableWidgetItem shown here:

# Insert the row at the table, on specified index
self.insertRow(index)

# Get the QTableWidgetItem from row r and column c, and ensure it exists
item = self.item(r, c)
if (item is None):
    item = QTableWidgetItem()
    self.setItem(r, c, item)

# To change the text color, using a QColor
QTableWidgetItem.setForeground()

# To change the text alignment, using Qt Flags
QTableWidgetItem.setTextAlignment() 

# To set the text inside the cell
QTableWidgetItem.setText()

My CSS, related to these widgets, is:

QTableWidget {
    border: 1px solid #ddd;
}

QTableWidget::item {
    background-color: white;
}

QTableWidget::item:alternate {
    background-color: #f1f1f1;
}

/* I Have already tried setting 'color: inherit', but there is no effect */
QTableView::item:selected, QTableView::item:alternate:selected {
    background-color: #92b7d1;
}

QHeaderView::section {
    background-color: lightgray;
}

QHeaderView::section:hover {
    background-color: silver;
}

Forgot to mention after posting: the rounded colored square, on 'Color' Column, is an Unicode Text from FontAwesomes, which I downloaded the OTF file and added to the application using the QFontDatabase from PySide2. Basically this guy here.

1

There are 1 answers

0
Carl HR On

For anyone still wondering, after an year or so, I found out that there exists a module that converts FontAwesome (and other sources of) icons into QIcons and even QWidgets.

Using that module, you can specify a color parameter and change the icon's color at will during its creation. Much, much easier than doing everything I've done on this post.

I recommend checking it out if you're going the through the route of this post.