1

I am developing a GUI in QT which performs some heavy computation based on user input . I doing all the computation in a function say start_computation() , as start_computation() function is called my GUI hangs since it is busy in executing start_computation() function . So, I want to use QProgressBar to let the user know that the GUI is performing some task in the background .

what will be the best way to implement QProgressBar in such case ?

Abhishek
  • 139
  • 2
  • 13
  • http://stackoverflow.com/questions/32952474/non-blocking-worker-interrupt-file-copy?lq=1 – dtech Mar 29 '16 at 09:52

3 Answers3

8

If the whole complex computation is handled by one function, it can be easily be ported to the QtConcurrent framework, doing something like the following, as mentioned in the tutorial linked below:

 QFuture<void> future = QtConcurrent::run(&this->MyObject, &MyClass::LongFunction);

A FutureWatcher object will then update the progress bar according to the future completion information.

There's a complete example on how to use QProgressBar with QFutures and QtConcurrent in general here: https://wiki.qt.io/Progress_Bar

Adding partial completion information to the progress bar will be trivial, emitting the appropriate information using the signals/slot mechanism.

alediaferia
  • 2,537
  • 19
  • 22
  • In the built-in example that is similar to the one on the wiki you linked to they use QtConcurrent::map() and connect the progress bar's setValue and cancel to signals emitted from the QFutureWatcher that aren't available when using QtConcurent::run(). Working from the linked example, would I instead simply add progress update a signal to MyClass that is emitted within LongFunction and connect that signal to the ProgressDialog setValue() in the form? Is it ok if MyClass is MainWindow itself? – oblivioncth Aug 16 '20 at 15:30
0

I don't know about your exact scenario but if your computation is one function ( start_computation()), you can use thread and QThread is a good solution.

In fact you need to put your heavy-computation in a new thread. Then you can check computation-progress from main(GUI) thread at times and update your progress bar.

PS: To see some example of implementing QThread in right way, you can refer to this SO question and Here.

Community
  • 1
  • 1
Emadpres
  • 3,466
  • 2
  • 29
  • 44
0

The best way to do it is to move your start_computation() function out into it's own thread, then using the signal/slot mechanism update the GUI correctly. The GUI has to always run on the main thread, and an intensive function will always block that.

There's a good discussion on how to use QThreads correctly here: http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55
  • I'd say to avoid messing around with `QThreads` when possible. There's a nice concurrent API in Qt provided by QtConcurrent module that can help with this kind of situation. Especially when there's a long-running function that does the heavy work and needs to be executed outside of the GUI thread. – alediaferia Mar 29 '16 at 08:42
  • I always forget about `QtConcurrent` for some reason, but yeah it's probably a lot more appropriate for this. – Nicholas Smith Mar 29 '16 at 10:15