How to set color of child QLabels when hovering parent QFrame on QSS?

I am trying to set the hover state color for 2 labels inside a QFrame using a stylesheet, but the frame accepts hover states regardless of whether there is an actual hover:

See screenshot: enter image description here

I have a QFrame with QLabels. I set the default colors to green and purple. When you hover over a QFrame, the color of both labels should turn red.

Exactly the same CSS works with html , but I can't get it to work with QT style sheets.

div{background-color:black; width:200px; height:100px;} #label1{color: green;} #label2{color: purple;} div:hover #label1 {color: red;} div:hover #label2 {color:red;} 
+3
source share
2 answers

You could not do this with QLabel . This is a limitation of QSS. Try using QToolButton instead of QLabel (and customize QToolButton to look like QLabel ).

Perhaps you are also using QToolButton instead of QFrame .

0
source

Tested with Qt5.9 and your code still does not work. I have the same behavior as you.

But with this simple QSS code, I get the label text in blue and red on hover.

 QLabel {color:blue} QFrame:hover {color:red} 

This does not work when using the object name, possibly due to the resolution of the conflict in QSS.

 #label {color:blue} QFrame:hover {color:red} 
0
source

Source: https://habr.com/ru/post/1499645/


All Articles