How do I tell my main GUI to wait on a worker thre

2019-08-05 13:14发布

I have successfully outsourced an expensive routine in my PyQT4 GUI to a worker QThread to prevent the GUI from going unresponsive. However, I would like the GUI to wait until the worker thread is finished processing to continue executing its own code.

The solution that immediately comes to my mind is to have the thread emit a signal when complete (as I understand, QThreads already do this), and then look for this signal in the main window before the rest of the code is executed. Is this hacked?

I know QThread provides the wait() function described here , but the usage is unclear to me. I think I want to call this on the main thread, but I'm not sure how to call that in my app...?

2条回答
淡お忘
2楼-- · 2019-08-05 14:00

This is a really bad plan. Split up the 'before thread action' and 'after thread action'. The 'after thread action' should be a slot fired by a QueuedConnection that the thread can signal.

Do not wait in GUI event handlers!

查看更多
疯言疯语
3楼-- · 2019-08-05 14:15

If the GUI thread called the wait() function on the worker thread object, it would not return from it until the worker thread returns from its main function. This is not what you want to do.

The solution you describe using signal and slots seems to make plenty of sense to me. Or you could just use a boolean.

查看更多
登录 后发表回答