看看有我的 Qwidget 对象,如果我在主线程上工作这个对象没有问题 Qwidget .show 正在工作,但如果我在其他线程上运行(threading.Thread)程序冻结并关闭。
我能做些什么 ?
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.
Put the code that you had running in a thread into a QObject.
Have that object emit signals and provide slots for interaction with UI. It should not be aware of any UI objects at all.
Connect that object to the UI object using signal/slot connections.
Move the object to another thread.
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.