1

I am developing a Qt desktop application now. I need to show user indeterminate progress bar while my program is computing. I can't determine time of computing and can't determine number of steps, that's why in my realization using of an indeterminate progress bar is the only option. I tried to use QProgressDialog. The only variant that worked (I mean showed user progress bar) was something like that:

QProgressDialog dialog("Computing", "Cancel", 0, 0);
dialog.setWindowModality(Qt::WindowModal);
dialog.exec();

//further code

But as you can understand, that further code didn't work while dialog was executing.

I also tried to use method show(), but Qt didn't render the dialog window, I mean during calculation window was transparent.

So, could you suggest some solutions that will help me to show user progress bar and compute at the same time? Computing time may vary very much.

dev-masih
  • 4,188
  • 3
  • 33
  • 55
cv_
  • 11
  • 5
  • If you do the long computation after the show() in the same thread you block the event loop and your progress bar is never painted. Calling processEvents() would be the ugly workaround (the UI will be still very sluggish), worker thread the clean solution. – Frank Osterfeld Nov 15 '15 at 11:01

1 Answers1

1

To show the progress of some work in parallel to responsive UI we usually have to have:

  1. UI thread not blocked by that parallel job
  2. The worker thread (QThread) performing the job
  3. UI should have Cancel button to stop the job
  4. UI progress bar shows fixed steps [0...X] or looping

If the goal is to show 'floating' progress: How to show an 'infinite floating' progressbar in Qt without knowing the percentage?

If the goal is to create the progress bar showing some 'realistic' degree of work done:

You have to estimate the full time somehow then. Do the experiment calculation and measure the full time. Assume that will be average time. Then make the progress driven by timer so it is going for 1/2 of progress length and if not done then it assumes the remaining time is twice longer. And then check at remaining 1/4 again and again expect twice longer. Do that until very small fraction of progress remains, say, around 1% but visible for the user and make it stay there until full calculation done. As you can guess when the job is done you advance the progress to the end showing the user that results ready.

To completely answer this question you will likely have to break it to several less "heavy" questions. Try the progress driven by timer first. Then try to create your math calculation job on a separate thread and make it signal to UI. When you have more specifics on certain implementation parts then C++/Qt programmers will likely be able to answer.

Here are some examples: tutorial #1, tutorial #2 on timer-driven Qt progress.

Community
  • 1
  • 1
Alexander V
  • 8,351
  • 4
  • 38
  • 47