StyleSheet on QWidget is not applied after hide/show
-
Hi,
I have a QWidget (which has no parents, it's a floating widget) on which I apply a very simple stylesheet to change the background color like this:
myWidget->setStyleSheet("QWidget{background-color:red;}");
This QWidget has to be visible when I launch my application, then I need to hide it and show it again later thanks to a button.
The stylesheet is correctly applied when I launch my application, but after I have hidden my widget once and show it again, the stylesheet is not applied anymore : the widget is shown as any default widget (white background). I have checked if the widget still had the stylesheet set, and it's correct.Do you have any ideas on why it happens?
Thanks a lot
-
Can't reproduce.
Try:#include <QApplication> #include <QPushButton> int main(int argc, char *argv[]) { QApplication a(argc,argv); QPushButton shoHideButton("Show/Hide"); QWidget styledWidget; styledWidget.setStyleSheet("background-color:red;"); styledWidget.setMinimumSize(200,200); QObject::connect(&shoHideButton, &QPushButton::clicked,&styledWidget,[&styledWidget]()->void{styledWidget.setVisible(!styledWidget.isVisible());}); styledWidget.show(); shoHideButton.show(); return a.exec(); }
EDIT
Thanks @kshegunov , you are right as always, still can't reproduce
-
Ok, actually I have found something else.
- if I use a manual button to show/hide the widget, its stylesheet is correctly applied
- if I try to create an "automatic" show/hide using some logic (an observer triggers when it rceives some data and shows the widget, so there is no direct manual interaction), then the widget is shown blank without stylesheet.
Do you have any ideas why ?
Thanks -
@ppetitpr said in StyleSheet on QWidget is not applied after hide/show:
Do you have any ideas why ?
Without some code to see what's happening it's a guessing game. So I'll guess, there's something in your logic that breaks it, no idea what.
-
@kshegunov said in StyleSheet on QWidget is not applied after hide/show:
@ppetitpr said in StyleSheet on QWidget is not applied after hide/show:
Do you have any ideas why ?
Without some code to see what's happening it's a guessing game. So I'll guess, there's something in your logic that breaks it, no idea what.
I couldn't reproduce my own issue with a standalone example outside of my application, so it was difficult to show code, but this has lead me to the reason: the show() called to show the widget was directly followed by a loop which blocked the GUI thread, just like that (very simplified):
myWidget.show(); for (i=0; i<BIG_NUMBER_HERE; ++i) { //do a lot of things that take time } myWidget.hide();
Of course Qt could not process anything between the show and the hide.
To validate this I added this line:QApplication::instance()->processEvents();
just after the call to show(), and now the stylesheet is correctly applied.
So, now that I have understood the problem, I think I will set the heavy loop to a working thread (it seems to be the way to go).
Thanks for your help !