0

=======================================================

QProgressBar* pbar = new QProgressBar(this);
pbar->setMinimum(0);
pbar->setMaximum(0);
pbar->show();
for (int i = 0; i < 10000; i++) {
    qDebug() << "=====flag1======";
}
pbar->close();

===========================================================

I want the ProgressBar show busy when qDebug() message, but there is no effect, the QProgressBar was blocked and close when the loop finished.

Does anyone know how to solve this problem? thank you!

  • 2
    Shouldn't you call `pbar->setValue(i)` in your `for` loop? Besides, you need to set progress bar maximum value to be more than minimum value - they are both zeros in your code. – vahancho Sep 11 '17 at 13:21
  • You may find this useful : [How to make Qt work when main thread is busy?](https://stackoverflow.com/questions/1386043/how-to-make-qt-work-when-main-thread-is-busy) – Simon Sep 11 '17 at 16:26
  • I know what you mean, but I don't want to exact progress value, I just want to show busy during loop – Amor_aeternus Dec 07 '17 at 09:55

3 Answers3

3

Yes GUI Is Blocked, becose 10 000 it's a lot of time. Use QThread http://doc.qt.io/qt-4.8/qthread.html .

void My_Thread::run() {
    for (int i = 0; i < 1e4; i++) {
        if (i % 100 == 0) {
            emit UpdateProgressBar(i);
        }
    }
}



//In Your SLOT
void MainWindow::UpdateProgressbar(int value) {
    ui->progressbar->setValue(value);
} 
Tazo leladze
  • 1,430
  • 1
  • 16
  • 27
1

Main threads is locked by loop before UI appears and UI is updated right after loop ends. If you want to see progress bar you can add QApplication::processEvents(); inside the loop. It's not the best solution but it will work.

Vova Shevchyk
  • 138
  • 10
1

To alow any widget to appear event loop has to be processed. Since you have full control over main thread its event loop is not able to process events which will show and update QProgressBar.

One way to fix if, which is quick but crapy is add to your loop QApplication::processEvents(); which process evens of event loop. Ofcaource you should also call bar->setValue(i);.

Proper way to do it is asynchronous programing which is using signals and slots. You didn't provide any details about your actual problem so can't provide good solution.

Marek R
  • 32,568
  • 6
  • 55
  • 140