Color priority between stylesheets and QBrush.
-
Hi. I have code that greys out the ui, including a QTableWidget. This is the sequence of the code:
fillCurrentTable(); ui_name::setEnabled(!greyState); ui->tableWidget->setEnabled(!greyState); ui->calibrationValue->setEnabled(!greyState); ui->tableWidget->setStyleSheet("font: 22px; color: grey;"); void ResistanceCalibration::fillCurrentTable(){ QBrush stableColor; stableMessage->setForeground(Qt::darkGreen); ui->tableWidget->setItem(x, 3, stableMessage); }
QBrush is run before setStyleSheet in the code. Therefore I would have thought sSS would override QBrush and paint everything in the QTable grey. However, this did not happen.
The result:
Does QBrush take priority under most circumstances? Please let me know if more information is required.
-
I would have guessed the same as @Christian-Ehrlicher. Probably the style sheet overrides the palette brushes.
My currently best explanation is that it is not the QBrush in itself, but the setForeground method which takes precedence. I first thought that setForeground is a general QWidget method or something. This is not the case. If you have a look at the documentation of QTableWidgetItem::setForeground (https://doc.qt.io/qt-6/qtablewidgetitem.html#setForeground) it specifically states:
Sets the item's foreground brush to the specified brush. Setting a default-constructed brush will let the view use the default color from the style.
So: Yes, for a QTableWidgetItem setForeground overrides any previous style.
-
As written in the documentation, style sheet always wins.
-
@Christian-Ehrlicher Thanks. In that case, what are some potential reasons that my QBrush seems to have taken priority? There are no other stylesheets taking priority over the QTable.
-
I would have guessed the same as @Christian-Ehrlicher. Probably the style sheet overrides the palette brushes.
My currently best explanation is that it is not the QBrush in itself, but the setForeground method which takes precedence. I first thought that setForeground is a general QWidget method or something. This is not the case. If you have a look at the documentation of QTableWidgetItem::setForeground (https://doc.qt.io/qt-6/qtablewidgetitem.html#setForeground) it specifically states:
Sets the item's foreground brush to the specified brush. Setting a default-constructed brush will let the view use the default color from the style.
So: Yes, for a QTableWidgetItem setForeground overrides any previous style.
-
@SimonSchroeder Thank you. In that case, I have two followup questions.
1: Are there any other ways to change the color of a QTableWidgetItem's text without using setForeground? I have not yet been able to find any on the QTableWidgetItem documentation and I don't see any on the document.
2: Are there ways of changing the stylesheets of individual rows/colums/cells? I'm assuming there aren't since I wasn't able to find any but I may be wrong. -
@Dummie1138
I feel there is some confusion here, which requires clarification. Qt stylesheet rules can only be applied toQWidget
s, andQTableWidgetItem
is not aQWidget
. So it cannot be addressed via any stylesheet rule.QTableWidget
is, of course, aQWidget
, so that can be stylesheeted, and it draws the items. You cannot do any stylesheeting to individual items. You can use aQStyledItemDelegate
for void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate) to control items' appearance. -
@JonB said in Color priority between stylesheets and QBrush.:
Qt stylesheet rules can only be applied to QWidgets, and QTableWidgetItem is not a QWidget. So it cannot be addressed via any stylesheet rule.
This is only partially correct. Yes, QTableWidgetItem is not a QWidget. However, when drawn Qt still applies a QWidget (or similar) internally. You can apply a stylesheet for
QTableWidgetItem
s by addressingQTableWidget::item
in the stylesheet. Though I am not aware that you can create a stylesheet per row/column/cell. This actually means you should use a QStyleItemDelegate as suggested.@JonB Maybe I misread your statement and need to apologize. Probably a better wording is: "So it cannot be addressed individually via any stylesheet rule."
-
@SimonSchroeder said in Color priority between stylesheets and QBrush.:
"So it cannot be addressed individually via any stylesheet rule."
Indeed. The OP was asking:
: Are there ways of changing the stylesheets of individual rows/colums/cells? I'm assuming there aren't since I wasn't able to find any but I may be wrong.
Each item is not a widget, cannot be individually addressed, cannot have, say, a dynamic property assigned to it so that you could alter it that way, etc.
::item
is documented as a "sub-control", with same standing as e.g.::icon
or::tab
.