Reputation: 4893
I have a QThread which does a lot of calculations (it can run for minutes), and at one (and only one) point it requires user input, for example in the form of a yes/no dialog. Of course, no GUI elements can be accessed and no dialogs opened from the thread (a Qt design choice), as it is not the main thread.
Well there are many obvious solutions, but I'm interested in a "recommended" solution, or a "best practice".
My ideas:
run()
method where the calculations are being done? If the run()
exits, the thread dies. So I have something like this in my run()
function: while (!can_continue) { sleep(); }
and I set can_continue
in the slot where I caught the signal sent form the main. However, I have some doubts about this being the most simple / most elegant solution. Is there a general practice I should know about?Upvotes: 1
Views: 1581
Reputation: 12557
Your problems with second version arises because you are working with Qt thread wrong.
You should create new object class Worker: public QObject
that has signals:
void stage1Finished();
void stage2Fibished();
and slots:
void startStage1();
void startStage2();
then create Qthread
thread object, push Worker
to the thread, connect startStage1()
with started()
signal of thread, show dialog on signal stage1Finished()
and connect dialog-accepted-signal with startStage2()
. Connect stage2Finished
with exit()
slot of thread.
Then you will not have to sleep anywhere, all signals processing will go through standard mechanism in Qthread
. Then you just start the thread to start processing and get finished
signal on processing finished.
While connecting signals to slots, use Qt::QueuedConnection
.
Upvotes: 3