Search code examples
cssqtstylesheet

Limiting the effects of a stylesheet to the parent widget?


I am setting the background color of a QWidget subclass. When I do this using a stylesheet, the styles of the other components in the QWidget change (eg, the color of a PushButton, and its look changes).

How do I make sure only the background color of the container widget changes and the rest of the child components remain unchanged?


Solution

  • One way is to specify an ID selector. Make sure to set the objectName of your container widget (with setObjectName()) and use that name in the CSS selector. Assuming a widget named MyContainer, you would use something like this:

    QWidget#MyContainer {...}