Didac Perez Parera
Didac Perez Parera

Reputation: 3814

Set a StyleSheet for a whole widget in Qt

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,

Upvotes: 3

Views: 3475

Answers (2)

Didac Perez Parera
Didac Perez Parera

Reputation: 3814

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

Upvotes: 1

fxam
fxam

Reputation: 3982

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

Upvotes: 4

Related Questions