How to apply style sheet to a custom widget in PyQt
In your project folder add a basic CSS file mystylesheet.css. Mutli-language editors like Atom are best for this type of things. The syntax highlighting works properly if you keep it named a CSS file.
Then drop the dot; qt knows what you mean.
mystylesheet.css
QWidget {
border: 20px solid black;
border-radius: 10px;
background-color: rgb(255, 255, 255);
}
anyQelement.setStyleSheet(open('mystylesheet.css').read())
Firstly: add an actual widget to your example:
self.widget = QWidget(self)
layout = QVBoxLayout(self)
layout.addWidget(self.widget)
Secondly: do yourself a favour, and use triple-quotes:
self.widget.setStyleSheet("""
QWidget {
border: 20px solid black;
border-radius: 10px;
background-color: rgb(255, 255, 255);
}
""")
The dot-selector in your example is redundant. What it does is specify that only instances of QWidget
itself will be selected, as opposed to sub-classes of QWidget
. See the StyleSheet Syntax guide in the Qt docs.