Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. StyleSheet on QWidget is not applied after hide/show
Forum Updated to NodeBB v4.3 + New Features

StyleSheet on QWidget is not applied after hide/show

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    ppetitpr
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      kshegunovK 1 Reply Last reply
      1
      • VRoninV VRonin

        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

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @VRonin, if that's your test code, then your styled widget is not native. You create it as a child of the native window.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • P Offline
          P Offline
          ppetitpr
          wrote on last edited by
          #4

          @VRonin It works with your code, which means the problem comes from inside my application, certainly something is intereacting improperly with my widget.

          Thanks

          1 Reply Last reply
          0
          • P Offline
            P Offline
            ppetitpr
            wrote on last edited by
            #5

            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

            kshegunovK J.HilkJ 2 Replies Last reply
            0
            • P ppetitpr

              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

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @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.

              Read and abide by the Qt Code of Conduct

              P 1 Reply Last reply
              2
              • P ppetitpr

                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

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @ppetitpr After quickly checking my crystal ball, my best guess is, you're calling new on your myWidget whenever new data arrives.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • kshegunovK kshegunov

                  @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.

                  P Offline
                  P Offline
                  ppetitpr
                  wrote on last edited by
                  #8

                  @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 !

                  1 Reply Last reply
                  2

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved