Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt QProgressBar indeterminate

Tags:

c++

qt

My application needs to do some operations which might take a second but might also take 10 minutes. For this purpose I need to show a QProgressDialog with indeterminate QProgressBar during the operation.

QProgressDialog dlg( this );
dlg.setBar( new QProgressBar() );
dlg->setMaximum( 0 );
dlg->setMinimum( 0 );
dlg.setModal( true );
dlg.show();

//operation ...

dlg.close();

During my operation the dialog shows up, is transparent, has no progressbar and after the operation it closes.

Does anyone know what I can do to show a modal dialog which prevents the user from interacting with the application and which shows the user an indeterminate progressbar?

like image 471
Davlog Avatar asked Feb 13 '23 13:02

Davlog


2 Answers

I'd suggest you don't create your own QProgressBar. The QProgressDialog has its own bar inside, and propagates all the methods from dialog to bar. What's more, to make your window modal use exec not show, or setModal(true) first. To close it, connect some signal (of a finished work) to the cancel() slot (or user will have to click Cancel button).

QProgressDialog dialog;
dialog.setRange(0,0);
dialog.exec();
like image 180
prajmus Avatar answered Feb 24 '23 06:02

prajmus


I think one thing that you might need is that you call QApplication::processEvents() while looping over your entries. Quoting from QCoreApplication docs:

You can call this function occasionally when your program is busy performing a long operation (e.g. copying a file).

and I think in this particular case the application will not update the appearance of your QProgressDialog while it is busy performing the long operation unless you call QApplication::processEvents().

If you have fixed range and you call setValue() as your loop progresses (quoting from the QProgressDialog docs):

If the progress dialog is modal (see QProgressDialog::QProgressDialog()), setValue() calls QApplication::processEvents()

(I am omitting here the warning which cautions that this can cause issues with re-entrancy).`

Note that when I tried your code it created a dialog like what you would expect if you only remove the line

dlg.setBar( new QProgressBar() );

As was said in another answer, QProgressDialog has its own QProgressBar so unless you have special requirements, this should do what you need.

like image 44
Erik Avatar answered Feb 24 '23 08:02

Erik