How to get QWidget background-color after set a QStyleSheet

1.9k views Asked by At

I have my desktop QApplication (Qt 5.9) on which I successfully set a style using a stylesheet. At runtime every QWidget look correctly styled as intended.
What I need to know is the background-color of a specific styled widget, let's say a QTooltip.

I tried the QWidget::palette method but what I got looked like the system's default color for the QPalette::ColorRole I asked.
Moreover, qt docs recommend not to use this method when using stylesheets.

So... more generally, how can one query for a widget property defined via stylesheets?

1

There are 1 answers

0
Petr On BEST ANSWER

You can't directly access individual properties of the style sheet. What you can do, is retrieve the style sheet and then you would need to parse it and find the property you are looking for (some_widget->styleSheet() would return it as a QString).

If you don't want to do that, and want to access the palette directly, you must use that instead of style sheet to change the colors.

Example code how you can style your code using palette instead of style sheet:

QPalette px;
px.setColor(QPalette::Text, QColor(255, 255, 255)); // Set text color to white
px.setColor(QPalette::Base, QColor(0, 0, 0)); // Set background to black
some_widget->setPalette(px);