Reputation: 85
After promoting my widget like the answer in this SO Link suggest I have trouble applying my Stylesheet on this specific widget. Please use the answer provided by @eyllanesc as working code.
I've read similar issues and some of them suggest handling this in the paintEvent?
I've tried to apply this stylesheet in both Mainform class and UiLoader class and also switching widget.setStyleSheet with self.setStyleSheet and put it in the paintEvent. I've also tried without the dot before QWidget and switching QWidget with QObject. None og these attempts seems to work.
widget.setStyleSheet("""
.QWidget{
border: 1px solid grey;
border-radius: 2px;
background-color: rgb(255, 255, 255);
}
""")
The stylesheet works in Designer, but the stylesheet doesn't get appliedwhen I'm running the program.
Upvotes: 2
Views: 389
Reputation: 244311
If you want a widget to support QSS you must do the painting using QStyle since it uses the QSS internally:
class Drawer(QtWidgets.QWidget):
def paintEvent(self, event):
opt = QtWidgets.QStyleOption()
opt.init(self)
qp = QtGui.QPainter(self)
self.style().drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, qp, self)
self.drawGeometry(qp)
qp.end()
def drawGeometry(self, qp):
qp.setPen(QtGui.QPen(QtCore.Qt.green, 8, QtCore.Qt.DashLine))
qp.drawEllipse(40, 40, 400, 400)
You also have to use the following QSS:
# ...
loader = UiLoader()
self.ui_window = loader.load(ui_file)
ui_file.close()
self.ui_window.show()
self.ui_window.widget.setStyleSheet(
"""
QWidget{
border: 1px solid grey;
border-radius: 2px;
background-color: rgb(255, 255, 255);
}
"""
)
# ...
Upvotes: 1