Search code examples
c++qtqt-creatorqprogressbar

how to implement QProgressBar for heavy computation?


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 ?


Solution

  • 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.