Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved [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.

    General and Desktop
    2
    2
    947
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      RaisinBread22 last edited by RaisinBread22

      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_())
      
      VRonin 1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin @RaisinBread22 last edited by

        @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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply Reply Quote 3
        • First post
          Last post