4

I created a button in Qt and gave it the QSS attribute background-color: gray;, while my external stylesheet has set the QSS attribute of the same button to background-color: blue;. When I run the application the button is gray, even though the style sheet is applied after the QWidget::show() is called and just before QApplication::exec(), as shown below:

MyWidget w;
w.show();
...
app.setStyleSheet("..."); // contents of external stylesheet
return app.exec();    

Is it possible to have QApplication::setStyleSheet() override the QSS attributes assigned to a Widget in Qt.

4
  • Your button should be blue, because app.setStyleSheet should redesign widget, can you show your full stylesheets?
    – Jablonski
    Sep 29, 2014 at 15:42
  • @Olumide Are you sure that your stylesheet is loaded correctly?
    – Max Go
    Sep 29, 2014 at 16:11
  • @N1ghtLight I am sure the stylesheet is loaded correctly. Actually, I am opening the stylesheet file reading it and writing its contents to QApplication::setStyleSheet()
    – Olumide
    Sep 29, 2014 at 16:12
  • @Olumide, please post here the piece of your external stylesheet where you define button background-color
    – Max Go
    Sep 29, 2014 at 16:22

1 Answer 1

5

No, it is not possible to override the QSS attributes the way you want, and trust me, you don't want to. It is not the order in which you call setStyleSheet that matters. It is the hierarchy that matters first. The call order matters only on widgets which are situated on the same level of the hierarchy.

The reason is that the widget has its internal style rules defined which override the parent's style thus the application style in your case. It is a hierarchy that is respected. You can look at this in the following way:

Say you have a QWidget with the following child hierarchy:

QWidget
  |__QPushButton
  |
  |__QFrame
  |  |
  |  |_QListView
  |
  |__QProgressBar

Let's say you want to customize the background-color to all the widgets in your hierarchy. If the call to QApplication::setStyleSheet() would overwrite the stylesheet properties for the children, it would be impossible for you to set a custom style for your children. That's why child widget's QSS properties overwrite parent widget's QSS properties.

Look at it like the usual way to look at widgets. QPushButton is shown on top of QWidget. QFrame is shown on top of QWidget. QListView is also shown on top of QWidget. Styles apply the same way.

What I recommend doing is having only one external QSS file in which you define everything you want.

EDIT: As N1ghtLight pointed out QSS preserves the class inheritance hierarchy so if you set a property for a class all its derived classes will inherit that property. For example if you have the following stylesheet:

QAbstractButton {
    background-color: red;
}

QPushButton {
    color: blue;
}

All QPushButtons will have the background color red and the text color blue as the QPushButton inherits the background-color property value from QAbstractButton which is its ancestor while QAbstractButtons which are not QPushButtons will have the background color red but the text color will remain unchanged.

The example above used a type selector. You can apply the style to specific objects by using different selector types. You can see different selector types here.

1
  • 2
    Will be nice if you will extend your answer with explanation how stylesheets provides possibility to define styles for base classes, for example, QAbstractButton, and these styles are inherited in inheritance hierarchy :)
    – Max Go
    Sep 29, 2014 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.