Search code examples
pythonqtpyqtpyqt5pyqt6

Pyqt5 QWidget.show() Not Working On Other Thread


Look There Are My Qwidget Object And If I Am Working This Object On Main Thread No Problem Qwidget .show is Working but If I Run On Other Thread(threading.Thread) program freezes and shuts down.

WHAT CAN I DO ?


Solution

  • 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.