Skip to content Skip to sidebar Skip to footer

Pyqt - How To Use The "setstylesheet" Method Properly?

if I use the setStyleSheet method in order to change the style for a specific widget, the other ones placed inside it, changes their style, but I don't want it! I can bring you two

Solution 1:

I strongly suggest you to more carefully read the style sheet syntax and reference documentation, as everything is clearly specified and explained there:

Style sheets consist of a sequence of style rules. A style rule is made up of a selector and a declaration. The selector specifies which widgets are affected by the rule; the declaration specifies which properties should be set on the widget.

Stylesheets are, by definition, cascading.

The stylesheet set on a widget is propagated on its children, those children inherit the style of the parent.

If you set a generic property like this:

border: 1px solid black;

The result is that all its children will have that border: you only gave the declaration but without the selector, so a universal selector is used as implicit.

This is not just Qt, this is typical of CSS (from which QSS take their main concepts), and it works exactly as any other widget styling property: setting a font or a palette on a widget, propagates them to all its children. The difference is that with stylesheets (exactly like standard CSS) you can use selectors.

If you want to style the tree widget only, then use that class selector:

    self.treeview.setStyleSheet('''
            QTreeView {
                border: 1px solid; 
                border-color:red;
            }
    ''')

Post a Comment for "Pyqt - How To Use The "setstylesheet" Method Properly?"