Set a StyleSheet for a whole widget in Qt

冷暖自知 提交于 2021-02-19 03:58:05

问题


I have a custom widget which inherits from QWidget and contains some labels in its layout. I would like to change the background color of the widget and the labels in the widget (this is, everything!) every time I put the mouse over it.

When using *:hover { background: red; } in my custom widget, I only get the contents red when moving the mouse over the labels, but not outside them, between labels, etc. I don't understand this behavior taking into account that I put the StyleSheet in the parent widget.

Any ideas? Many thanks,


回答1:


You can set the parent's stylesheet which will cascade to children like this:

parent->setStyleSheet("* {background: red}");

For hovering only:

parent->setStyleSheet("*:hover {background: red}");

Check out https://qt-project.org/doc/qt-5.1/qtwidgets/stylesheet-syntax.html




回答2:


Finally I solved the problem creating a QFrame inside the main QWidget and setting the StyleSheet of that QFrame.



来源:https://stackoverflow.com/questions/19097632/set-a-stylesheet-for-a-whole-widget-in-qt

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