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. changing progressbar value few times inside one function

changing progressbar value few times inside one function

Scheduled Pinned Locked Moved Solved General and Desktop
qprogressbar
11 Posts 4 Posters 1.3k Views
  • 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.
  • C Corvette653
    9 Feb 2021, 08:02

    Hi, I have a problem with QProgressBar

    void MainWindow::on_pushButton_clicked()
    {
        QProgressBar *progressBar = new QProgressBar;
    
        progressBar->setRange(0, 100);
        progressBar->setValue(1);
        progressBar->show();
    
        for (int i = 1; i < 11; i++)
        {
            progressBar->setValue(10*i);
            QThread::msleep(100);
        }
    }
    

    I expected this function to increase progressBar value every 0.1sec, but it creates QProgressBar, sleeps a whole second and sets progressBar value instantly to 100.

    Detailed description:
    I have a function which can last even over a minute, and want to create a QProgressBar for user to know, that program hasn't stopped, and windows not to display a warning "application stopped running. do you want to close it?".
    Everything would be nice, if progressBar updated instantly, but it only shows with 0 value till funcion end

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 9 Feb 2021, 08:05 last edited by jsulm 2 Sept 2021, 08:06
    #2

    @Corvette653 said in changing progressbar value few times inside one function:

    I expected this function to increase progressBar value every 0.1sec, but it creates QProgressBar, sleeps a whole second and sets progressBar value instantly to 100.

    Well, it does not work this way - you are blocking Qt event loop with your for loop and msleep()!
    If you have a long lasting operation then move it to a thread. In that thread you can emit a signal to tell the UI to update the progress bar (but don't do it in that thread!).

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

    1 Reply Last reply
    3
    • C Offline
      C Offline
      Corvette653
      wrote on 9 Feb 2021, 08:12 last edited by
      #3

      @jsulm
      Could you write a minimal compilable example?

      J J 2 Replies Last reply 9 Feb 2021, 08:17
      0
      • C Corvette653
        9 Feb 2021, 08:12

        @jsulm
        Could you write a minimal compilable example?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 9 Feb 2021, 08:17 last edited by
        #4

        @Corvette653 said in changing progressbar value few times inside one function:

        Could you write a minimal compilable example?

        I could. But I will not.
        Please start by reading documentation (and examples) to learn how to do it.
        https://doc.qt.io/qt-5/qthread.html
        https://doc.qt.io/qt-5/signalsandslots.html

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

        1 Reply Last reply
        5
        • C Corvette653
          9 Feb 2021, 08:12

          @jsulm
          Could you write a minimal compilable example?

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 9 Feb 2021, 08:24 last edited by
          #5

          @Corvette653 because I have one ready made.

          https://github.com/DeiVadder/QtThreadExample


          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
          3
          • C Offline
            C Offline
            Corvette653
            wrote on 9 Feb 2021, 09:50 last edited by
            #6

            @J-Hilk
            Thank you so much, I have analized your project.
            I am wondering if there is any sense in making workerObject?
            Fourth and fifth solution are beyond my capacity, so I try to choose between workerThread and Thread + workerObject

            J 1 Reply Last reply 10 Feb 2021, 07:58
            0
            • S Offline
              S Offline
              SimonSchroeder
              wrote on 10 Feb 2021, 07:30 last edited by
              #7

              Just as others have said before, you need to use threads to have the progress dialog update in real time. One common workaround (so that you don't have to use threads) is to call QApplication::processEvents() occasionally. However, this will really slow down your loop by multiple factors and is not advisable for interactive applications.

              If you use threads you have to make sure that every call to methods of QProgressBar is done inside the GUI thread. Otherwise your application will crash. One way to schedule a call inside the GUI event loop is to use QMetaObject::invokeMethod(qApp, ...). This can quickly become quite unreadable, especially when you do it over and over again. That is why we wrote a little header-only library to simplify this task: https://github.com/SimonSchroeder/QtThreadHelper. Have a look at Use Case 2 in the description. It will show how to use this tiny library to update the progress.

              1 Reply Last reply
              0
              • C Corvette653
                9 Feb 2021, 09:50

                @J-Hilk
                Thank you so much, I have analized your project.
                I am wondering if there is any sense in making workerObject?
                Fourth and fifth solution are beyond my capacity, so I try to choose between workerThread and Thread + workerObject

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 10 Feb 2021, 07:58 last edited by
                #8

                @Corvette653 said in changing progressbar value few times inside one function:

                @J-Hilk
                Thank you so much, I have analized your project.
                I am wondering if there is any sense in making workerObject?
                Fourth and fifth solution are beyond my capacity, so I try to choose between workerThread and Thread + workerObject

                There are some differences, mostly it's easier to set up and not to mess up. Plus, you can very easily switch between using a thread and mot using a thread by simply not moving the object to the new thread instance.

                In the end it's up to you.


                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
                0
                • C Offline
                  C Offline
                  Corvette653
                  wrote on 10 Feb 2021, 09:25 last edited by
                  #9

                  @J-Hilk
                  Ok, one more time, thank you so much.

                  Comparing your example with documentation, shouldn't you use finished signal there?
                  https://github.com/DeiVadder/QtThreadExample/blob/6b28ba239fbbe83b530e5850df279bfe67a64f55/mainwindow.cpp#L106

                  J 1 Reply Last reply 10 Feb 2021, 09:31
                  0
                  • C Corvette653
                    10 Feb 2021, 09:25

                    @J-Hilk
                    Ok, one more time, thank you so much.

                    Comparing your example with documentation, shouldn't you use finished signal there?
                    https://github.com/DeiVadder/QtThreadExample/blob/6b28ba239fbbe83b530e5850df279bfe67a64f55/mainwindow.cpp#L106

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 10 Feb 2021, 09:31 last edited by
                    #10

                    @Corvette653 in this particular case you could do that, it would result in the same outcome

                    However, if you, in the thread sub class, uncomment the exec(), finished will never be emitted on its own


                    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
                    0
                    • C Offline
                      C Offline
                      Corvette653
                      wrote on 10 Feb 2021, 09:38 last edited by
                      #11

                      In my programm, I got

                      QThread: Destroyed while thread is still running
                      

                      when I was using operationDone, changing into finished relolved this problem

                      1 Reply Last reply
                      1

                      11/11

                      10 Feb 2021, 09:38

                      • Login

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