QProgressDialog doesn't update
-
I am using the example from http://doc.qt.io/qt-5/qprogressdialog.html. The dialog will show only if I call
exec()
, but there will be displayed only an empty progress bar. I also useprocessEvents()
inside the loop. -
Hi
The exact code ? -
This is it:
QApplication a(argc, argv);
QProgressDialog progress("Copying files...", "Abort Copy", 0, 10); progress.setWindowModality(Qt::WindowModal); progress.exec(); for (int i = 1; i <= 10; i++) { QCoreApplication::processEvents(); progress.setValue(i); if (progress.wasCanceled()) break; std::cout<<"i: "<<i<<"\n"; } progress.close();```
-
Hi,
@DoubleC122 said in QProgressDialog doesn't update:
progress.exec();
for (int i = 1; i <= 10; i++) {exec
starts an event loop, so currently, yourfor
loop is only executed after the dialog is closed. -
Call show on the dialog.
Just in case, QProgressDialog's documentation offers some example on how to use it.
-
Yes, but unfortunately, it won't appear when I call
show()
. -
Something I didn't realise... Your loop is just too short and too fast for the dialog to appear.
-
Hm, I thought it would appear anyway...Still, I changed the limit to something bigger, like 100 and 1000, and still won't show. Quite strange.
-
@DoubleC122
Well its just still too fast as it do no real work.
It does show very fast. (flashing)for test, you can do
#include <QThread> ... progress.show(); for (int i = 1; i <= 13330; i++) { QCoreApplication::processEvents(); progress.setValue(i); if (progress.wasCanceled()) { break; } QThread::sleep(1); // bad in real code! but just to show it does work. } progress.close();
-
@DoubleC122
yes, if it does real stuff like copy file or anything that takes time.
even with high for loop it just too fast to see :)
But if u do something real, it works. Thats what i used sleep to emulate.
but dont use sleep for anything production code as u sleep the app .) -
Got it. Thanks.