Search code examples
qtqprogressbar

increment progress bar inside for loop in c++ Qt


I am new in Qt , i want to show some progress in progress bar in side a for loop, progress bar should show progress from 0 to 100 How to do this ,plz help thanks.


Solution

  • From the documentation (e.g. http://doc.qt.io/qt-4.8/qprogressbar.html)

    A progress bar is used to give the user an indication of the progress of an operation [...]

    You can specify the minimum and maximum number of steps with setMinimum() and setMaximum. The current number of steps is set with setValue().

    So what you need is to construct a QProgressBar object, specify what the minimum and maximum should be and then call setValue(int value) to make it progress.

    For your case:

    QProgressBar progressBar;
    progressBar.setMinimum(0);
    progressBar.setMaximum(100);
    // or as alternative to the two above, you could call
    // progressBar.setRange(0,100);
    
    for( int i = 0; i <100; ++i ) {
       progressBar.setValue(i);
    }