Using Qt Progress Dialogs

March 26, 2015

At work I have been fighting with Qt's implementation of Progress Dialogs. I have used Android's implementation before which was easy enough, but I could not seem to get Qt's to work right.

I found the key to understanding the behavior of the dialog is to understand that updating the progress bar requires an Event Loop. I was failing by assuming that using the code below would create a seperate thread

QTimer::singleShot(0, fileData.get(), SLOT(load()));

While this works to show a dialog, there is no event loop open for the progress dialog, so the UI of the dialog is never drawn (Seriously, this gave me a transparent widget). So I relaized that I have the loader in a seperate thread while the progress dialog spins. So I changed it to the following code, where file data is an smart pointer of type FileDataClass.

QFuture loadThread = QtConcurrent::run(fileData.get(), &FileDataClass::load);

Which gives me this build error:

error: no matching function for call to ‘run(FileDataClass*, bool (FileDataClass::*)(QString))’
    QFuture loadThread = QtConcurrent::run( fileData.get(), &FileDataClass::load);

Hmmm. load is an overloaded function which means apparently QtConcurrent doesn't like overloaded functions. Let's try adding a function parameter to the run call.

QFuture loadThread = QtConcurrent::run(fileData.get(), &FileDataClass::load, "path/to/file");

It works (Well assuming you have a callback somehwere calling reset on the progressDialog to stop it from spinning). So, to recap, a progress dialog may be updated from a seperate thread and closed from that same seperate thread to avoid any blocking or stalling of the program. This way we leave the progress dialog updating in the main event loop while we run the function we want to show the progress of in a different thread. The progress dialog picks up the reset call because the main event loop checks for events before each pass updating the progress dialog.

comments powered by Disqus
  • Copyright 2013, Matthew Iannucci
  • Site created with, version 0.3
  • Updated January 2015