[Interest] QProgressDialog not showing & processEvents()

André Somers andre at familiesomers.nl
Mon Nov 18 08:09:41 CET 2013


Etienne Sandré-Chardonnal schreef op 15-11-2013 14:05:
> Dear all,
>
> I have a long file saving function (gigabytes - several minutes) and I 
> am trying to use QProcessDialog. I am using it the Modal way as 
> explained in the documentation. However, the dialog appears after 
> about 50 seconds, despite having set the minimumDuration to 0, and 
> called setValue() several times before.
>
> Googling for the issue says I need to call QApplication::processEvents 
> regularly in order for the GUI to refresh. However, the doc says :
>
> "If the progress dialog is modal (seeQProgressDialog::QProgressDialog 
> <qthelp://com.trolltech.qt.481/qdoc/qprogressdialog.html#QProgressDialog>()), 
> setValue() callsQApplication::processEvents 
> <qthelp://com.trolltech.qt.481/qdoc/qcoreapplication.html#processEvents>(), 
> so take care that this does not cause undesirable re-entrancy in your 
> code"
>
> So I'm puzzled... Do I need to call it, or is it already called by 
> setValue? In the example there are no explicit calls to processEvents().
>
> Qt 4.8.1
It is *not* called by setValue. That is a Good Thing(TM). In fact, I 
think the whole call is dangerous to begin with.

You have two viable approaches here:
* break up the saving method into chunks. That is, do a small part of 
the work of the saving, queue the next piece of work to be done, and 
return to the eventloop. The eventloop will then trigger the next queued 
piece of the work being done. This way, there is no need for 
processEvents() at all.
* move your saving code to a worker object, and execute it in a separate 
thread. Also, no need to use processEvents, as the heavy lifting is done 
outside of the GUI thread.

Note that this does not only go for saving or loading, but it goes for 
any task that may block your GUI thread for a substantial amount of time.

André



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131118/79f0a1ae/attachment.html>


More information about the Interest mailing list