How to set color of child QLabels while hover parent QFrame with QSS?

瘦欲@ 提交于 2019-12-10 15:56:32

问题


I'm trying to set the hover state color of 2 labels inside a QFrame with a stylesheet, but the frame takes the hover states regardless if there is an actual hover:

See screenshot:

I've got a QFrame with QLabels. I set the default colors to green and purple. While i hover the QFrame, the color of both labels should go red.

The exact same CSS does work with html, but i can't seem to get it to work with the QT stylesheets.

div{background-color:black; width:200px; height:100px;}

#label1{color: green;}
#label2{color: purple;}

div:hover #label1 {color: red;}
div:hover #label2 {color:red;}

回答1:


You could not do it with QLabel. It's limitation of QSS. Try to use QToolButton instead of QLabel (and customize QToolButton to look like QLabel).

Possible you shuld use QToolButton instead of QFrame too.




回答2:


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

But, with this simple QSS code I get the label's text to be blue, and red when hovered.

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

This does not work when using the object-name, probably because of the conflict resolution of QSS.

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


来源:https://stackoverflow.com/questions/21911820/how-to-set-color-of-child-qlabels-while-hover-parent-qframe-with-qss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!