-4

看看有我的 Qwidget 对象,如果我在主线程上工作这个对象没有问题 Qwidget .show 正在工作,但如果我在其他线程上运行(threading.Thread)程序冻结并关闭。

我能做些什么 ?

4

1 回答 1

0

In a word: don’t do it. It doesn’t even have to do with threads, everything to do with separation of the UI and the “business” logic.

  1. Put the code that you had running in a thread into a QObject.

  2. Have that object emit signals and provide slots for interaction with UI. It should not be aware of any UI objects at all.

  3. Connect that object to the UI object using signal/slot connections.

  4. Move the object to another thread.

  5. Start the thread.

And you’ve totally avoided the problem now. But make sure that the business logic object never knows about any UI objects: it can’t directly interact with them, since it’ll be in the wrong thread and it won’t be appropriately decoupled from the UI.

于 2021-03-19T18:56:37.670 回答