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. how to make the progress bar run smoothly

how to make the progress bar run smoothly

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 3.2k 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.
  • B Offline
    B Offline
    Blackzero
    wrote on last edited by
    #1

    how do you make the progress bar run smoothly, I mean smooth is if the initial value is 0 then the process value jumps to 50 the progress bar runs from 0 to 50 smoothly instead of jumping directly from 0 to 50, I hope you understand, this is an example of code from C# that makes the progress bar smooth, this is not a thread problem but how to make the progress bar run smoothly

    private void SetProgressBarPosition(ProgressBar progressBar, int position)
    {
    	bool invokeRequired = progressBar.InvokeRequired;
    	if (invokeRequired)
    	{
    		FormMain.SetProgressBarPositionCallBack method = new FormMain.SetProgressBarPositionCallBack(this.SetProgressBarPosition);
    		progressBar.Invoke(method, new object[]
    	{
    		progressBar,
    		position
    	});
    	}
    	else
    	{
    		progressBar.Value = position;
    	}
    }
    
    JonBJ Pl45m4P 3 Replies Last reply
    0
    • B Blackzero

      how do you make the progress bar run smoothly, I mean smooth is if the initial value is 0 then the process value jumps to 50 the progress bar runs from 0 to 50 smoothly instead of jumping directly from 0 to 50, I hope you understand, this is an example of code from C# that makes the progress bar smooth, this is not a thread problem but how to make the progress bar run smoothly

      private void SetProgressBarPosition(ProgressBar progressBar, int position)
      {
      	bool invokeRequired = progressBar.InvokeRequired;
      	if (invokeRequired)
      	{
      		FormMain.SetProgressBarPositionCallBack method = new FormMain.SetProgressBarPositionCallBack(this.SetProgressBarPosition);
      		progressBar.Invoke(method, new object[]
      	{
      		progressBar,
      		position
      	});
      	}
      	else
      	{
      		progressBar.Value = position;
      	}
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Blackzero
      We can't say anything from this code. What range does position take, how often do you call it with what values, does it make any difference whether invokeRequired is true or false, do you run this in a different thread from the main thread, ... ? The progressbar does "run smoothly" if you set its value correctly and over time. Try it in C++ or Python.

      1 Reply Last reply
      1
      • B Blackzero

        how do you make the progress bar run smoothly, I mean smooth is if the initial value is 0 then the process value jumps to 50 the progress bar runs from 0 to 50 smoothly instead of jumping directly from 0 to 50, I hope you understand, this is an example of code from C# that makes the progress bar smooth, this is not a thread problem but how to make the progress bar run smoothly

        private void SetProgressBarPosition(ProgressBar progressBar, int position)
        {
        	bool invokeRequired = progressBar.InvokeRequired;
        	if (invokeRequired)
        	{
        		FormMain.SetProgressBarPositionCallBack method = new FormMain.SetProgressBarPositionCallBack(this.SetProgressBarPosition);
        		progressBar.Invoke(method, new object[]
        	{
        		progressBar,
        		position
        	});
        	}
        	else
        	{
        		progressBar.Value = position;
        	}
        }
        
        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @Blackzero said in how to make the progress bar run smoothly:

        how do you make the progress bar run smoothly

        Simple:

        Connect a signal from your non-blocking "work" task to QProgressBar::setValue(int).
        If the task whose status is represented by the progressbar is blocking and unblocks at 50% first, you will see that jump.
        ( And also, if you have no progress and suddenly set a value of 50... the progress bar will go from 0 to 50 instantly )

        Say you have a worker with some intense computing task

        // #####################
        // Worker class in different thread
        for ( int i = 0; i < 100; ++i ) {
           QThread::sleep(1); // simulate 1s of "work"
           int progress = i;
           emit progressDone(progress); 
        }
        // ####################
        
        
        // #######################
        // MainWindow where ProgressBar is
        
        QProgressBar *progressBar = new QProgressBar(this);
        progressBar->setRange(0, 100);
        Worker w;
        // worker setup here
        // ...
        connect (&w, &Worker::progressDone, this, [=](int progress)
                                 { progressBar->setValue(progress); });
        
        
        

        This whole thing also works without any worker thread... if you want to update your bar, just use progressBar->setValue(progress) to set your progress value. Then you have that "jump" again.

        See also the QProgressDialog documentation:

        • https://doc.qt.io/qt-6/qprogressdialog.html#details

        There is shown how to update the bar and show the actual progress of some task.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        JonBJ B 2 Replies Last reply
        1
        • Pl45m4P Pl45m4

          @Blackzero said in how to make the progress bar run smoothly:

          how do you make the progress bar run smoothly

          Simple:

          Connect a signal from your non-blocking "work" task to QProgressBar::setValue(int).
          If the task whose status is represented by the progressbar is blocking and unblocks at 50% first, you will see that jump.
          ( And also, if you have no progress and suddenly set a value of 50... the progress bar will go from 0 to 50 instantly )

          Say you have a worker with some intense computing task

          // #####################
          // Worker class in different thread
          for ( int i = 0; i < 100; ++i ) {
             QThread::sleep(1); // simulate 1s of "work"
             int progress = i;
             emit progressDone(progress); 
          }
          // ####################
          
          
          // #######################
          // MainWindow where ProgressBar is
          
          QProgressBar *progressBar = new QProgressBar(this);
          progressBar->setRange(0, 100);
          Worker w;
          // worker setup here
          // ...
          connect (&w, &Worker::progressDone, this, [=](int progress)
                                   { progressBar->setValue(progress); });
          
          
          

          This whole thing also works without any worker thread... if you want to update your bar, just use progressBar->setValue(progress) to set your progress value. Then you have that "jump" again.

          See also the QProgressDialog documentation:

          • https://doc.qt.io/qt-6/qprogressdialog.html#details

          There is shown how to update the bar and show the actual progress of some task.

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

          @Pl45m4
          Yep, except all this needs writing for C#, where I don't even know if/how they have emit, connect(), threads, etc. :)

          Pl45m4P 1 Reply Last reply
          0
          • JonBJ JonB

            @Pl45m4
            Yep, except all this needs writing for C#, where I don't even know if/how they have emit, connect(), threads, etc. :)

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @JonB said in how to make the progress bar run smoothly:

            Yep, except all this needs writing for C#

            I think @Blackzero just wanted to refer to the C# snippet he is familiar with and where everything works as expected.
            Now he wants to know how to do this in C++ with QProgressBar?!
            But maybe I'm wrong.
            The code above is WinForms C# code... no Qt involved there...


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            JonBJ 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @JonB said in how to make the progress bar run smoothly:

              Yep, except all this needs writing for C#

              I think @Blackzero just wanted to refer to the C# snippet he is familiar with and where everything works as expected.
              Now he wants to know how to do this in C++ with QProgressBar?!
              But maybe I'm wrong.
              The code above is WinForms C# code... no Qt involved there...

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

              @Pl45m4
              Not my understanding. The only reason for him showing SetProgressBarPosition() as C# code is if he is using the C# bindings for Qt, else he wouldn't have even come across it.... I think :)

              1 Reply Last reply
              0
              • B Blackzero

                how do you make the progress bar run smoothly, I mean smooth is if the initial value is 0 then the process value jumps to 50 the progress bar runs from 0 to 50 smoothly instead of jumping directly from 0 to 50, I hope you understand, this is an example of code from C# that makes the progress bar smooth, this is not a thread problem but how to make the progress bar run smoothly

                private void SetProgressBarPosition(ProgressBar progressBar, int position)
                {
                	bool invokeRequired = progressBar.InvokeRequired;
                	if (invokeRequired)
                	{
                		FormMain.SetProgressBarPositionCallBack method = new FormMain.SetProgressBarPositionCallBack(this.SetProgressBarPosition);
                		progressBar.Invoke(method, new object[]
                	{
                		progressBar,
                		position
                	});
                	}
                	else
                	{
                		progressBar.Value = position;
                	}
                }
                
                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @JonB I think this:

                @Blackzero said in how to make the progress bar run smoothly:

                progressBar.Value = position;

                equals this

                • https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.progressbar.value

                I guess... but... whatever... no idea. That's the reason why one should create understandable topics and ask clear questions...

                ¯\_(ツ)_/¯

                @JonB said in how to make the progress bar run smoothly:

                else he wouldn't have even come across it.... I think :)

                Port existing C# WinForms App to Qt and C++?!
                Found working C# snippet online, but no idea how to translate this to C++...
                Many reasons ;-)

                You see people asking "I have this Python code. What is this in C++?" on StackOverflow regularly...
                Understandable that so many people are annoyed there. ;-)

                But back to the topic :)
                Only @Blackzero can answer all this, everything else is just guesswork :)


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @Blackzero said in how to make the progress bar run smoothly:

                  how do you make the progress bar run smoothly

                  Simple:

                  Connect a signal from your non-blocking "work" task to QProgressBar::setValue(int).
                  If the task whose status is represented by the progressbar is blocking and unblocks at 50% first, you will see that jump.
                  ( And also, if you have no progress and suddenly set a value of 50... the progress bar will go from 0 to 50 instantly )

                  Say you have a worker with some intense computing task

                  // #####################
                  // Worker class in different thread
                  for ( int i = 0; i < 100; ++i ) {
                     QThread::sleep(1); // simulate 1s of "work"
                     int progress = i;
                     emit progressDone(progress); 
                  }
                  // ####################
                  
                  
                  // #######################
                  // MainWindow where ProgressBar is
                  
                  QProgressBar *progressBar = new QProgressBar(this);
                  progressBar->setRange(0, 100);
                  Worker w;
                  // worker setup here
                  // ...
                  connect (&w, &Worker::progressDone, this, [=](int progress)
                                           { progressBar->setValue(progress); });
                  
                  
                  

                  This whole thing also works without any worker thread... if you want to update your bar, just use progressBar->setValue(progress) to set your progress value. Then you have that "jump" again.

                  See also the QProgressDialog documentation:

                  • https://doc.qt.io/qt-6/qprogressdialog.html#details

                  There is shown how to update the bar and show the actual progress of some task.

                  B Offline
                  B Offline
                  Blackzero
                  wrote on last edited by
                  #8

                  @Pl45m4 said in how to make the progress bar run smoothly:

                  Hubungkan sinyal dari tugas "pekerjaan" non-pemblokiran Anda ke QProgressBar::setValue(int).
                  Jika tugas yang statusnya diwakili oleh bilah kemajuan memblokir dan membuka blokir pada 50% terlebih dahulu, Anda akan melihat lompatan tersebut.
                  (Dan juga, jika Anda tidak mengalami kemajuan dan tiba-tiba menetapkan nilai 50... bilah kemajuan akan langsung berubah dari 0 menjadi 50)

                  Katakanlah Anda memiliki seorang pekerja dengan tugas komputasi yang intens

                  I use multi-threading and there is no problem with threads but my process adjusts the transfer speed why the value is sometimes not arranged from 1 to 100 so how can I make the progressbar run like a normal process even though the value jumps from 0 to 30 or 50, I hope you understand what I mean with this confusing question

                  Pl45m4P 1 Reply Last reply
                  0
                  • B Blackzero

                    @Pl45m4 said in how to make the progress bar run smoothly:

                    Hubungkan sinyal dari tugas "pekerjaan" non-pemblokiran Anda ke QProgressBar::setValue(int).
                    Jika tugas yang statusnya diwakili oleh bilah kemajuan memblokir dan membuka blokir pada 50% terlebih dahulu, Anda akan melihat lompatan tersebut.
                    (Dan juga, jika Anda tidak mengalami kemajuan dan tiba-tiba menetapkan nilai 50... bilah kemajuan akan langsung berubah dari 0 menjadi 50)

                    Katakanlah Anda memiliki seorang pekerja dengan tugas komputasi yang intens

                    I use multi-threading and there is no problem with threads but my process adjusts the transfer speed why the value is sometimes not arranged from 1 to 100 so how can I make the progressbar run like a normal process even though the value jumps from 0 to 30 or 50, I hope you understand what I mean with this confusing question

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #9

                    @Blackzero

                    You still did not mention if you are talking about QProgressBar and C++ code.
                    Why the WinForms C# example?

                    @Blackzero said in how to make the progress bar run smoothly:

                    how can I make the progressbar run like a normal process even though the value jumps from 0 to 30 or 50
                    I hope you understand what I mean with this confusing question

                    Sorry no I dont. The QProgressBar does not "run". You have to control the progress yourself. There is no animation or something like that by default, so that you see a "flow".
                    After some progress is done, you have to update the bar (and set/increase the value).


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    B 1 Reply Last reply
                    0
                    • Pl45m4P Pl45m4

                      @Blackzero

                      You still did not mention if you are talking about QProgressBar and C++ code.
                      Why the WinForms C# example?

                      @Blackzero said in how to make the progress bar run smoothly:

                      how can I make the progressbar run like a normal process even though the value jumps from 0 to 30 or 50
                      I hope you understand what I mean with this confusing question

                      Sorry no I dont. The QProgressBar does not "run". You have to control the progress yourself. There is no animation or something like that by default, so that you see a "flow".
                      After some progress is done, you have to update the bar (and set/increase the value).

                      B Offline
                      B Offline
                      Blackzero
                      wrote on last edited by
                      #10

                      @Pl45m4 said in how to make the progress bar run smoothly:

                      Sorry no I dont. The QProgressBar does not "run". You have to control the progress yourself. There is no animation or something like that by default, so that you see a "flow".
                      After some progress is done, you have to update the bar (and set/increase the value).

                      okay I have found a way, yes it uses QPropertyAnimation

                      void MainWindow::UpdateProgresBar(int value)
                      {
                          QPropertyAnimation *animation = new QPropertyAnimation(ui->progressBar, "value");
                          animation->setDuration(1000); 
                          animation->setStartValue(ui->progressBar->value());
                          animation->setEndValue(value);
                          animation->setEasingCurve(QEasingCurve::Linear); 
                          animation->start(QAbstractAnimation::DeleteWhenStopped);
                      }
                      
                      JonBJ Pl45m4P 2 Replies Last reply
                      1
                      • B Blackzero

                        @Pl45m4 said in how to make the progress bar run smoothly:

                        Sorry no I dont. The QProgressBar does not "run". You have to control the progress yourself. There is no animation or something like that by default, so that you see a "flow".
                        After some progress is done, you have to update the bar (and set/increase the value).

                        okay I have found a way, yes it uses QPropertyAnimation

                        void MainWindow::UpdateProgresBar(int value)
                        {
                            QPropertyAnimation *animation = new QPropertyAnimation(ui->progressBar, "value");
                            animation->setDuration(1000); 
                            animation->setStartValue(ui->progressBar->value());
                            animation->setEndValue(value);
                            animation->setEasingCurve(QEasingCurve::Linear); 
                            animation->start(QAbstractAnimation::DeleteWhenStopped);
                        }
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @Blackzero
                        So by using this it moves from empty to full in precisely 1 second without any input/messages about the progress of whatever it is supposed to track (such as progress in a computation or whatever). So it isn't a "progress" bar any more, it's just an animation, didn't know that is what you really wanted.

                        Pl45m4P 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @Blackzero
                          So by using this it moves from empty to full in precisely 1 second without any input/messages about the progress of whatever it is supposed to track (such as progress in a computation or whatever). So it isn't a "progress" bar any more, it's just an animation, didn't know that is what you really wanted.

                          Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on last edited by
                          #12

                          @JonB said in how to make the progress bar run smoothly:

                          empty to full in precisely 1 second without any input/messages

                          If @Blackzero code works as expected, the animated bar will start from current value and end on the value which is passed to the function. So not 0 -100% every time?!


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          JonBJ 1 Reply Last reply
                          1
                          • Pl45m4P Pl45m4

                            @JonB said in how to make the progress bar run smoothly:

                            empty to full in precisely 1 second without any input/messages

                            If @Blackzero code works as expected, the animated bar will start from current value and end on the value which is passed to the function. So not 0 -100% every time?!

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

                            @Pl45m4 OIC, I didn't look closely (at value), yes, you are right. I won't delete but I will strike out my post.

                            1 Reply Last reply
                            1
                            • B Blackzero

                              @Pl45m4 said in how to make the progress bar run smoothly:

                              Sorry no I dont. The QProgressBar does not "run". You have to control the progress yourself. There is no animation or something like that by default, so that you see a "flow".
                              After some progress is done, you have to update the bar (and set/increase the value).

                              okay I have found a way, yes it uses QPropertyAnimation

                              void MainWindow::UpdateProgresBar(int value)
                              {
                                  QPropertyAnimation *animation = new QPropertyAnimation(ui->progressBar, "value");
                                  animation->setDuration(1000); 
                                  animation->setStartValue(ui->progressBar->value());
                                  animation->setEndValue(value);
                                  animation->setEasingCurve(QEasingCurve::Linear); 
                                  animation->start(QAbstractAnimation::DeleteWhenStopped);
                              }
                              
                              Pl45m4P Offline
                              Pl45m4P Offline
                              Pl45m4
                              wrote on last edited by
                              #14

                              @Blackzero said in how to make the progress bar run smoothly:

                              okay I have found a way, yes it uses QPropertyAnimation

                              The whole guessing could have been avoided if you have said that you are looking for an animated QProgressBar.
                              Nobody knew what you had in mind when you were showing the C# code and said you want your progress bar to "run smoothly" ;-)


                              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                              ~E. W. Dijkstra

                              B 1 Reply Last reply
                              1
                              • Pl45m4P Pl45m4

                                @Blackzero said in how to make the progress bar run smoothly:

                                okay I have found a way, yes it uses QPropertyAnimation

                                The whole guessing could have been avoided if you have said that you are looking for an animated QProgressBar.
                                Nobody knew what you had in mind when you were showing the C# code and said you want your progress bar to "run smoothly" ;-)

                                B Offline
                                B Offline
                                Blackzero
                                wrote on last edited by
                                #15

                                @Pl45m4 said in how to make the progress bar run smoothly:

                                Tidak ada yang tahu apa yang ada dalam pikiran Anda ketika Anda menunjukkan kode C# dan mengatakan Anda ingin bilah kemajuan Anda "berjalan lancar" ;-)

                                I find it hard to explain because I don't know much about Qt.

                                1 Reply Last reply
                                0
                                • B Blackzero has marked this topic as solved on

                                • Login

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