user5636914
user5636914

Reputation:

QProgressBar and heavy task

I have heavy task. I created QProgressBar for displaying that programm still work and runned heavy task in another thread using c++ std::thread class. But QProgressBar don't work, only window with QProgressBar starts. Here is the code:

QProgressBar progress;
progress.setRange(0, 0);
progress.show();
if (keyLength == 1024)
    std::thread(&RSA::generateKeys, &rsa, RSA::RSA_1024).join();
else if (keyLength == 2048)
    std::thread(&RSA::generateKeys, &rsa, RSA::RSA_2048).join();

Here is the result: enter image description here

Upvotes: 0

Views: 1143

Answers (1)

rbaleksandar
rbaleksandar

Reputation: 9701

This is not how things are done. :) Here you can see an example I have made that uses the Worker pattern (a separate thread that processes some heavy task and reports back to the UI). Here is how my application looks:

enter image description here

I use QThread (worker thread that contains an object that handles the processing) and I can only recommend you to do the same. You can also subclass QThread and override the run() method depending on what you really need however this is rarely the case.

PS: As an alternative you can use QRunnable (very useful for tasks that are done every once in a while which doesn't require a separate thread to be managed all the time). The problem with QRunnable is that it doesn't subclass QObject which means that you can't use the slot-signal mechanism to report back to the UI. Of course you can change that but it defeats the purpose of the runnable which is intended to be a very lightweight solution.

Upvotes: 2

Related Questions