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. QProgressBar has rendering errors or does not render at all
Forum Updated to NodeBB v4.3 + New Features

QProgressBar has rendering errors or does not render at all

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qt6
19 Posts 6 Posters 1.5k Views 2 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.
  • Y Offline
    Y Offline
    YuXin
    wrote on last edited by
    #1

    Why does this progress bar always have rendering errors or fail to render at all? Unless the content of the text is not updated or the graphical effects of the text are removed.

    The code is like this:

    #include <QGraphicsDropShadowEffect>
    #include <QProgressBar>
    #include <QApplication>
    #include <QVBoxLayout>
    #include <QDateTime>
    #include <QLabel>
    
    class Test : public QWidget {
        QProgressBar pb;
        QLabel text;
        QVBoxLayout layout{this};
    public:
        Test(): pb(this), text(this) {
            layout.addWidget(&text, 0, Qt::AlignCenter);
            layout.addWidget(&pb);
            pb.setMaximum(3600);
            /*  Setting it to 50ms is just to see the anomaly more quickly.
                Changing it to 500ms can still reproduce the issue.
            */
            startTimer(50);
            auto g = new QGraphicsDropShadowEffect;
            g->setBlurRadius(10);// It affects both heoccurence and severity of this condition.
            g->setOffset(0, 0);
            g->setColor(Qt::white);
            text.setGraphicsEffect(g);
            text.setStyleSheet(
                "font-size: 24px;"
                "font-weight: bold;"
                "color: rgb(66, 66, 66);"
            );
            pb.setTextVisible(false);
        }
        void timerEvent(QTimerEvent* event) override {
            if (QDateTime::currentMSecsSinceEpoch() % 2)
                pb.setValue(pb.value() + 1);//If the number 1 is changed to a number greater than or equal to 20, it will work normally
            static int counter = 1;
            text.setText(QString::number(counter++));
        }
    };
    
    int main(int argc, char** argv) {
        QApplication app(argc,argv);
        Test t;
        t.show();
        return app.exec();
    }
    

    I think it’s because some unknown reason caused the content drawn by the repaint called in the setValue of QProgressBar to be lost (as if it was never drawn at all). The upper part of the pb progress bar was correctly drawn because the text used a graphical effect, which increased the drawing range and thus drew that part of the pb progress bar.

    The result of the execution is like this:

    1

    I'm certain that the paintEvent of pb has been executed, because I tried writing a class to test it, and the result is that it indeed has been executed and continues to be executed constantly.

    class ProgressBar : public QProgressBar {
    public:
        ProgressBar(QWidget* parent = nullptr): QProgressBar(parent) {}
    
        void paintEvent(QPaintEvent* event) {
            static int counter = 1;
            QProgressBar::paintEvent(event);
            qDebug() << "paintEvent(QPaintEvent*)" << counter++;
        }
    };
    

    The code has been tested on Linux with Qt 6.9.0 and 6.5.5, as well as on Windows with Qt 6.9.0. The results were either rendering errors or no rendering at all.

    From https://stackoverflow.cm/questions/79568310/qprogressba-has-rendering-errors-or-does-not-render-at-all

    JonBJ 1 Reply Last reply
    0
    • Y YuXin

      Why does this progress bar always have rendering errors or fail to render at all? Unless the content of the text is not updated or the graphical effects of the text are removed.

      The code is like this:

      #include <QGraphicsDropShadowEffect>
      #include <QProgressBar>
      #include <QApplication>
      #include <QVBoxLayout>
      #include <QDateTime>
      #include <QLabel>
      
      class Test : public QWidget {
          QProgressBar pb;
          QLabel text;
          QVBoxLayout layout{this};
      public:
          Test(): pb(this), text(this) {
              layout.addWidget(&text, 0, Qt::AlignCenter);
              layout.addWidget(&pb);
              pb.setMaximum(3600);
              /*  Setting it to 50ms is just to see the anomaly more quickly.
                  Changing it to 500ms can still reproduce the issue.
              */
              startTimer(50);
              auto g = new QGraphicsDropShadowEffect;
              g->setBlurRadius(10);// It affects both heoccurence and severity of this condition.
              g->setOffset(0, 0);
              g->setColor(Qt::white);
              text.setGraphicsEffect(g);
              text.setStyleSheet(
                  "font-size: 24px;"
                  "font-weight: bold;"
                  "color: rgb(66, 66, 66);"
              );
              pb.setTextVisible(false);
          }
          void timerEvent(QTimerEvent* event) override {
              if (QDateTime::currentMSecsSinceEpoch() % 2)
                  pb.setValue(pb.value() + 1);//If the number 1 is changed to a number greater than or equal to 20, it will work normally
              static int counter = 1;
              text.setText(QString::number(counter++));
          }
      };
      
      int main(int argc, char** argv) {
          QApplication app(argc,argv);
          Test t;
          t.show();
          return app.exec();
      }
      

      I think it’s because some unknown reason caused the content drawn by the repaint called in the setValue of QProgressBar to be lost (as if it was never drawn at all). The upper part of the pb progress bar was correctly drawn because the text used a graphical effect, which increased the drawing range and thus drew that part of the pb progress bar.

      The result of the execution is like this:

      1

      I'm certain that the paintEvent of pb has been executed, because I tried writing a class to test it, and the result is that it indeed has been executed and continues to be executed constantly.

      class ProgressBar : public QProgressBar {
      public:
          ProgressBar(QWidget* parent = nullptr): QProgressBar(parent) {}
      
          void paintEvent(QPaintEvent* event) {
              static int counter = 1;
              QProgressBar::paintEvent(event);
              qDebug() << "paintEvent(QPaintEvent*)" << counter++;
          }
      };
      

      The code has been tested on Linux with Qt 6.9.0 and 6.5.5, as well as on Windows with Qt 6.9.0. The results were either rendering errors or no rendering at all.

      From https://stackoverflow.cm/questions/79568310/qprogressba-has-rendering-errors-or-does-not-render-at-all

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @YuXin said in QProgressBar has rendering errors or does not render at all:

          if (QDateTime::currentMSecsSinceEpoch() % 2)
              pb.setValue(pb.value() + 1);
      

      This is probably quite irrelevant to whatever your issue is. But as a "reproducer" I don't understand: you have no idea whether the if will be true always/never/somewhere in between. Or will vary from run to run. If it's always false it will never be hit. It might work for you in practice but not for someone else.

      Y 1 Reply Last reply
      0
      • JonBJ JonB

        @YuXin said in QProgressBar has rendering errors or does not render at all:

            if (QDateTime::currentMSecsSinceEpoch() % 2)
                pb.setValue(pb.value() + 1);
        

        This is probably quite irrelevant to whatever your issue is. But as a "reproducer" I don't understand: you have no idea whether the if will be true always/never/somewhere in between. Or will vary from run to run. If it's always false it will never be hit. It might work for you in practice but not for someone else.

        Y Offline
        Y Offline
        YuXin
        wrote on last edited by
        #3

        @JonB This was actually reflected in the testing of the paintEvent calls, but I'm not sure why hard-coding the condition in this if statement would prevent it from being reproduced.

        JonBJ 1 Reply Last reply
        0
        • Y YuXin

          @JonB This was actually reflected in the testing of the paintEvent calls, but I'm not sure why hard-coding the condition in this if statement would prevent it from being reproduced.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @YuXin
          You have no idea/guarantee what number QDateTime::currentMSecsSinceEpoch() will return each time (and hence whether it will be odd or even always/never/sometimes), you cannot rely on that even matching a QTimer event in real time. [With your choice of startTimer(50); it's likely to be either always true or always false, I would have thought. At least startTimer(49) would more likely alternate.] Just saying, leave it at that.

          Y 1 Reply Last reply
          0
          • JonBJ JonB

            @YuXin
            You have no idea/guarantee what number QDateTime::currentMSecsSinceEpoch() will return each time (and hence whether it will be odd or even always/never/sometimes), you cannot rely on that even matching a QTimer event in real time. [With your choice of startTimer(50); it's likely to be either always true or always false, I would have thought. At least startTimer(49) would more likely alternate.] Just saying, leave it at that.

            Y Offline
            Y Offline
            YuXin
            wrote on last edited by YuXin
            #5

            @JonB The timing of the startTimer does not affect the issue. Regardless of how it is adjusted, the problem can still be reproduced. Even if it is increased tenfold to 500, the issue persists—it's just that a shorter interval allows the problem to occur more quickly.

            Additionally, the purpose of this  if  statement is exactly as you assumed: to randomly trigger  setValue , rather than being aligned with the 50-interval setting.

            JonBJ 1 Reply Last reply
            0
            • Y YuXin

              @JonB The timing of the startTimer does not affect the issue. Regardless of how it is adjusted, the problem can still be reproduced. Even if it is increased tenfold to 500, the issue persists—it's just that a shorter interval allows the problem to occur more quickly.

              Additionally, the purpose of this  if  statement is exactly as you assumed: to randomly trigger  setValue , rather than being aligned with the 50-interval setting.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @YuXin said in QProgressBar has rendering errors or does not render at all:

              The timing of the startTimer does not affect the issue.

              In principle an odd number is more likely to trigger QDateTime::currentMSecsSinceEpoch() % 2 being true on alternates than always being either true or false.

              the purpose of this if statement is exactly as you assumed: to randomly trigger setValue

              Exactly. And for that you would be much better off using a random number than your potentially-deterministic QDateTime::currentMSecsSinceEpoch() % 2 .

              Anyway, I am sorry if we digressed. I intended this to be a brief comment, especially for posting for others to reproduce.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Which version of Qt are you using ?
                On which OS ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                Y 1 Reply Last reply
                1
                • SGaistS SGaist

                  Hi,

                  Which version of Qt are you using ?
                  On which OS ?

                  Y Offline
                  Y Offline
                  YuXin
                  wrote on last edited by YuXin
                  #8

                  @SGaist This question has been answered in the page.

                  SGaistS 1 Reply Last reply
                  0
                  • Y YuXin

                    @SGaist This question has been answered in the page.

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @YuXin Sorry, I missed that line.

                    Could you test with a more recent version of Qt ?

                    One small note: you are using a pretty large range which means that you won't necessarily see the painting done immediately. While it's likely unrelated to the issue at hand, it adds an unnecessary delay to test your issue.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Y 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      @YuXin Sorry, I missed that line.

                      Could you test with a more recent version of Qt ?

                      One small note: you are using a pretty large range which means that you won't necessarily see the painting done immediately. While it's likely unrelated to the issue at hand, it adds an unnecessary delay to test your issue.

                      Y Offline
                      Y Offline
                      YuXin
                      wrote on last edited by
                      #10

                      @SGaist The fact that the same issue occurred during the testing of version 6.9.0 clearly indicates that upgrading the version is useless. I also tested the currently latest version, 6.10, and the result is that the same problem persists.

                      SGaistS 1 Reply Last reply
                      0
                      • hskoglundH Offline
                        hskoglundH Offline
                        hskoglund
                        wrote on last edited by
                        #11

                        Hi, it seems you have a race condition, i.e. the graphics effect and the progress bar are fighting each other, and currently the graphics effect is winning.

                        Try giving the progress bar the upper hand by telling it to repaint, say like this:

                        ...
                        // add a repaint call
                        void timerEvent(QTimerEvent* event) override {
                                if (QDateTime::currentMSecsSinceEpoch() % 2)
                                {
                                    pb.setValue(pb.value() + 1);//If the number 1 is changed to a number greater than or equal to 20, it will work normally
                                    pb.repaint();
                                }
                                static int counter = 1;
                                text.setText(QString::number(counter++));
                        }
                        ...
                        
                        Y 2 Replies Last reply
                        0
                        • Y YuXin

                          @SGaist The fact that the same issue occurred during the testing of version 6.9.0 clearly indicates that upgrading the version is useless. I also tested the currently latest version, 6.10, and the result is that the same problem persists.

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @YuXin said in QProgressBar has rendering errors or does not render at all:

                          @SGaist The fact that the same issue occurred during the testing of version 6.9.0 clearly indicates that upgrading the version is useless. I also tested the currently latest version, 6.10, and the result is that the same problem persists.

                          No, it only indicates that there's an issue with the version you tested against.

                          Since it still happens with 6.10, then there's indeed something to investigate.

                          Try the suggestion of @hskoglund and if does not improve the situation, then check the bug report system and if you don't find anything there, please open a new ticket providing your minimal example with the adjustments people suggested on this thread.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          Y 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            @YuXin said in QProgressBar has rendering errors or does not render at all:

                            @SGaist The fact that the same issue occurred during the testing of version 6.9.0 clearly indicates that upgrading the version is useless. I also tested the currently latest version, 6.10, and the result is that the same problem persists.

                            No, it only indicates that there's an issue with the version you tested against.

                            Since it still happens with 6.10, then there's indeed something to investigate.

                            Try the suggestion of @hskoglund and if does not improve the situation, then check the bug report system and if you don't find anything there, please open a new ticket providing your minimal example with the adjustments people suggested on this thread.

                            Y Offline
                            Y Offline
                            YuXin
                            wrote last edited by
                            #13

                            @SGaist that's ok

                            1 Reply Last reply
                            0
                            • hskoglundH hskoglund

                              Hi, it seems you have a race condition, i.e. the graphics effect and the progress bar are fighting each other, and currently the graphics effect is winning.

                              Try giving the progress bar the upper hand by telling it to repaint, say like this:

                              ...
                              // add a repaint call
                              void timerEvent(QTimerEvent* event) override {
                                      if (QDateTime::currentMSecsSinceEpoch() % 2)
                                      {
                                          pb.setValue(pb.value() + 1);//If the number 1 is changed to a number greater than or equal to 20, it will work normally
                                          pb.repaint();
                                      }
                                      static int counter = 1;
                                      text.setText(QString::number(counter++));
                              }
                              ...
                              
                              Y Offline
                              Y Offline
                              YuXin
                              wrote last edited by
                              #14

                              @hskoglund First of all, I sincerely apologize for the delayed reply—I’ve been extremely busy.

                              Secondly, your proposal is indeed feasible, but I’d like to know what you mean by "race condition"—I’ve never heard of it before.

                              Is there any documentation that explains this?

                              jsulmJ 1 Reply Last reply
                              0
                              • Y YuXin

                                @hskoglund First of all, I sincerely apologize for the delayed reply—I’ve been extremely busy.

                                Secondly, your proposal is indeed feasible, but I’d like to know what you mean by "race condition"—I’ve never heard of it before.

                                Is there any documentation that explains this?

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote last edited by
                                #15

                                @YuXin https://en.wikipedia.org/wiki/Race_condition

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • hskoglundH hskoglund

                                  Hi, it seems you have a race condition, i.e. the graphics effect and the progress bar are fighting each other, and currently the graphics effect is winning.

                                  Try giving the progress bar the upper hand by telling it to repaint, say like this:

                                  ...
                                  // add a repaint call
                                  void timerEvent(QTimerEvent* event) override {
                                          if (QDateTime::currentMSecsSinceEpoch() % 2)
                                          {
                                              pb.setValue(pb.value() + 1);//If the number 1 is changed to a number greater than or equal to 20, it will work normally
                                              pb.repaint();
                                          }
                                          static int counter = 1;
                                          text.setText(QString::number(counter++));
                                  }
                                  ...
                                  
                                  Y Offline
                                  Y Offline
                                  YuXin
                                  wrote last edited by YuXin
                                  #16

                                  @hskoglund I looked into it: when changing the progress bar value, it internally calls
                                  repaint, which should trigger an immediate redraw.

                                  On the other hand, the label uses
                                  update, but the call to
                                  update shouldn’t be executed until the
                                  repaint is completed. Moreover, by the time this timer event starts, the previous drawing of the label should already have been completed.

                                  Why would there be a race condition in this scenario?

                                  1 Reply Last reply
                                  0
                                  • Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote last edited by
                                    #17

                                    There is no race condition but you use QGraphicsDropShadowEffect for a child widget with a radius of 10 so this widget is responsible for drawing everything of it's size + 10 pixel which overlaps with your progressbar.
                                    I don't understand why you use QGraphicsDropShadowEffect with this large radius at all. What do you want to achieve for a plain QWidget not in a QGraphicsScene?

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    Y 1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Christian Ehrlicher

                                      There is no race condition but you use QGraphicsDropShadowEffect for a child widget with a radius of 10 so this widget is responsible for drawing everything of it's size + 10 pixel which overlaps with your progressbar.
                                      I don't understand why you use QGraphicsDropShadowEffect with this large radius at all. What do you want to achieve for a plain QWidget not in a QGraphicsScene?

                                      Y Offline
                                      Y Offline
                                      YuXin
                                      wrote last edited by
                                      #18

                                      @Christian-Ehrlicher I also think there is no race condition here, but can you tell me why their overlapping drawing areas would cause this? I'm really curious about the reason, even though I already know the solution.

                                      1 Reply Last reply
                                      0
                                      • Christian EhrlicherC Offline
                                        Christian EhrlicherC Offline
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote last edited by
                                        #19

                                        As I said - the effect draws outside it's client area.

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        1 Reply Last reply
                                        0

                                        • Login

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