I have created a custom widget and applied the following stylesheet to it (via QtDesigner):
QWidget#MyWidget{
background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0
rgb(200, 200, 200), stop:1 rgb(230, 230, 230))
}
QWidget#MyWidget:hover{
background:
qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #90B750,
stop:1#CAFF70 )
}
It works perfekt in the designer.
In another custom widget, I add a lot of those MyWidget instances to a Vertical layout. But neither the background color is set according to the stylesheet, nor does the hovering work. For testing purposes, I added a pushbutton to MyWidget. Hovering and clicking works. What am I missing?
Basically MyWidget is just a widget with a few labels, the "container" is just a ScrollArea with a vertical layout and again a few labels.
Any ideas?
Turns out, solution is fairly simple.
The problem is, that apparently, if you parent a widget to another widget, the background of the top-most widget is set to "transparent". Adding a second widget which has the main widget as parent and applying the QStylesheet to this widget solves the problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.