[Pyside2/PyQt5] Moving an object to QThread, which calls methods of other objects whose classes I've defined, which I want to emit signals from.



  • I'm new to Qt and threading so I've been reading various Stack overflow posts, Qt documentation pages and, blog posts on multithreading in Qt to beetter understand how to implement multithreading properly into my application. Multithreading is necessary in my application in order to call an object of a class that uses pynput to monitor user input for a hotkey combination. The pynput key monitoring would block the Qt main event loop otherwise.

    I think this stackoverflow answer has a relevant example that could guide me in implementing multithreading, specifically the logic inside the using_move_to_thread() function. However, there's a problem. In the example moveToThread(objThread) is called on an instance of the Somebject class and that class creates and emits a custom signal finished. However, what if inside the class Someobject there was an instantiation of an object of another user defined class and the programmer wanted to emit a signal from a method of that object. Since that object wasn't created in the main event loop thread, but instead in an object that lives in another scope & thread, you can't refer like you did with obj.finished.connect(objThread.quit) which was created in the main Qt event loop thread & scope.

    Here's the same exact code that's in the stackoverflow answer I referenced earlier. I've excluded everything that doesn't apply to my question:

    import sys
    import time
    
    from PyQt5.QtCore import (QCoreApplication, QObject, QRunnable, QThread,
                              QThreadPool, pyqtSignal)
    
    
    
    class SomeObject(QObject):
    
        finished = pyqtSignal()
    
        def long_running(self):
            count = 0
            while count < 5:
                time.sleep(1)
                print("B Increasing")
                count += 1
            self.finished.emit()
    
    
    
    
    if __name__ == "__main__":
         app = QCoreApplication([])
        objThread = QThread()
        obj = SomeObject()
        obj.moveToThread(objThread)
        obj.finished.connect(objThread.quit)
        objThread.started.connect(obj.long_running)
        objThread.finished.connect(app.exit)
        objThread.start()
        sys.exit(app.exec_())
    


  • @RaisinBread22 said in [Pyside2/PyQt5] Moving an object to QThread, which calls methods of other objects whose classes I've defined, which I want to emit signals from.:

    However, what if inside the class Someobject there was an instantiation of an object of another user defined class and the programmer wanted to emit a signal from

    Just define an identical signal as the instantiated class in Someobject and connect, from the constructor of Someobject, the signal in the instance with the signal in Someobject


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.