Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple example using QProgressDialog: Any ideas why this doesn't work properly?

Tags:

qt

I am trying to make a simple example with QProgressDialog. So I have one QPushButton in a widget and when I press it, a QProgressDialog appears, progresses until 100 and then hides.

My problem is that if I click the save button for the second time, the QProgressDialog just appears and disappears.

The code of my slot that is called when the user presses the button:

progressDialog->show();
progressDialog->setWindowModality(Qt::WindowModal);
for(int i = 0; i <= 100; ++i)
{
    progressDialog->setValue(i);

    if(progressDialog->wasCanceled())
        break;
}
like image 303
Chrys Avatar asked Dec 06 '11 11:12

Chrys


1 Answers

You need to allow the GUI to update/redraw itself. It doesn't do that on every setXXX call as it is too expensive and unnecessary. However, a redraw() event is queued so if you allow Qt to process events, the dialog will update.

Simply add the following inside your for loop, and read it's documentation for further usage scenarios:

 QApplication::processEvents();

Also, the loop you have runs far too fast for anything to be shown. Add a sleep call or do some useful work in order to see anything.

Update (thx Tim)

You might want to use QApplication::processEvents( QEventLoop::ExcludeUserInputEvents) to disallow user events (there's also an option for network events). They may trigger other parts in your application and cause re-entrancy and other unwanted control-flow.

But in your case, you probably want to include user events to get the cancel button.

like image 65
Macke Avatar answered Sep 24 '22 15:09

Macke