Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. pyqt signal not emitted when button is clicked after move to QThread

pyqt signal not emitted when button is clicked after move to QThread

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 2 Posters 3.4k Views
  • 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.
  • J Offline
    J Offline
    JokZiz1
    wrote on last edited by
    #1

    Essentially I have a procedure, that will start upon the button click. Once I start the procedure, everything works fine, until the user input is required. However, when the user clicks the button, no 'clicked' signal is emitted. Signal is connected to slot appropriately. Button click stoped working after I moved the code to QThread.

    class Procedure(QObject):
        
        def __init__(self, parent):
            super().__init__()
            self.parent = parent
    
            self.parent.button_a.clicked.connect(self.on_button_a_clicked)
            self.event = threading.Event()
    
        def run(self):
            # started running, doing some stuff here
    
            # waits for button click, i.e. when button is clicked, the event is set and then you may proceed
            self.event.wait()
            # NEVER REACHES HERE
    
        def on_button_a_clicked(self):
            self.event.set()
    
    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)
            self.setFixedSize(self.size())
    
            self.start_button.clicked.connect(self.on_start_clicked)
    
        def on_start_clicked(self):
            self.thread = QThread()
            self.worker = Procedure(self)
            self.worker.moveToThread(self.thread)
    
            self.thread.started.connect(self.worker.run)
            self.thread.start()
    
    def main():
    
        app = QtWidgets.QApplication(sys.argv)
        window = MainWindow()
        window.show()
        app.exec_()
    
    
    if __name__ == '__main__':
        main()
    

    However, I do have an indication, that the signal is connected to slot appropriately, as when in function run() I manually emit the signal, the button click is emulated successfully. Therefore, I presume that the issue is that button click is not registered appropriately.

        def run(self):
            # started running, doing some stuff here
    
            # following line successfully emulates the button click
            self.parent.button_a.clicked.emit()
            self.event.wait()
            # reaches here successfully
    

    I also presume that this has something to do with QThread, since the issue appeared after I started to run my procedure in a QThread and all of the buttons that are related to main window function well, but I am kind of lost in the woods here and I am not sure how to debug this issue. Thank you in advance.

    jsulmJ 1 Reply Last reply
    0
    • J JokZiz1

      Essentially I have a procedure, that will start upon the button click. Once I start the procedure, everything works fine, until the user input is required. However, when the user clicks the button, no 'clicked' signal is emitted. Signal is connected to slot appropriately. Button click stoped working after I moved the code to QThread.

      class Procedure(QObject):
          
          def __init__(self, parent):
              super().__init__()
              self.parent = parent
      
              self.parent.button_a.clicked.connect(self.on_button_a_clicked)
              self.event = threading.Event()
      
          def run(self):
              # started running, doing some stuff here
      
              # waits for button click, i.e. when button is clicked, the event is set and then you may proceed
              self.event.wait()
              # NEVER REACHES HERE
      
          def on_button_a_clicked(self):
              self.event.set()
      
      class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
      
          def __init__(self):
              super(MainWindow, self).__init__()
              self.setupUi(self)
              self.setFixedSize(self.size())
      
              self.start_button.clicked.connect(self.on_start_clicked)
      
          def on_start_clicked(self):
              self.thread = QThread()
              self.worker = Procedure(self)
              self.worker.moveToThread(self.thread)
      
              self.thread.started.connect(self.worker.run)
              self.thread.start()
      
      def main():
      
          app = QtWidgets.QApplication(sys.argv)
          window = MainWindow()
          window.show()
          app.exec_()
      
      
      if __name__ == '__main__':
          main()
      

      However, I do have an indication, that the signal is connected to slot appropriately, as when in function run() I manually emit the signal, the button click is emulated successfully. Therefore, I presume that the issue is that button click is not registered appropriately.

          def run(self):
              # started running, doing some stuff here
      
              # following line successfully emulates the button click
              self.parent.button_a.clicked.emit()
              self.event.wait()
              # reaches here successfully
      

      I also presume that this has something to do with QThread, since the issue appeared after I started to run my procedure in a QThread and all of the buttons that are related to main window function well, but I am kind of lost in the woods here and I am not sure how to debug this issue. Thank you in advance.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @JokZiz1 said in pyqt signal not emitted when button is clicked after move to QThread:

      when the user clicks the button, no 'clicked' signal is emitted

      Which button do you mean? You have two.
      Also, why does child Procedure connect parents signal? This is bad design! Child should not know anything about parent. Such connections should be done by parent. I mean this connection:

      self.parent.button_a.clicked.connect(self.on_button_a_clicked)
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JokZiz1
        wrote on last edited by
        #3

        @jsulm said in pyqt signal not emitted when button is clicked after move to QThread:

        @JokZiz1 said in pyqt signal not emitted when button is clicked after move to QThread:

        when the user clicks the button, no 'clicked' signal is emitted

        Which button do you mean? You have two.

        The button called 'button_a' does not work. The button that starts the thread does work.

        Moving signal-slot connection from child to parent did not solve the issue

        jsulmJ 1 Reply Last reply
        0
        • J JokZiz1

          @jsulm said in pyqt signal not emitted when button is clicked after move to QThread:

          @JokZiz1 said in pyqt signal not emitted when button is clicked after move to QThread:

          when the user clicks the button, no 'clicked' signal is emitted

          Which button do you mean? You have two.

          The button called 'button_a' does not work. The button that starts the thread does work.

          Moving signal-slot connection from child to parent did not solve the issue

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JokZiz1 Please add debug output to on_button_a_clicked to see whether it is called or not. Also, for signals/slots across threads you should use QueuedConnection.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JokZiz1
            wrote on last edited by
            #5

            on_button_a_clicked is not called, that is why I have made this post. Also, I don't see how QueuedConnection helps me, unless you could elaborate as I am not using signals/slots accross threads, everything is done in a single thread...

            jsulmJ 1 Reply Last reply
            0
            • J JokZiz1

              on_button_a_clicked is not called, that is why I have made this post. Also, I don't see how QueuedConnection helps me, unless you could elaborate as I am not using signals/slots accross threads, everything is done in a single thread...

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @JokZiz1 Please show how you're connecting button_a.clicke now. The slot should be called if connect() succeeds.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply
              0
              • jsulmJ jsulm

                @JokZiz1 Please show how you're connecting button_a.clicke now. The slot should be called if connect() succeeds.

                J Offline
                J Offline
                JokZiz1
                wrote on last edited by JokZiz1
                #7

                @jsulm said in pyqt signal not emitted when button is clicked after move to QThread:

                @JokZiz1 Please show how you're connecting button_a.clicke now. The slot should be called if connect() succeeds.

                If you would have read what I have initially written, the signal is connected to slot appropriately, because if I manually in the code emit the clicked signal, it is reached then.

                Anyway, the changes I have made

                
                class Procedure(QObject):
                    
                    def __init__(self, parent):
                        super().__init__()
                        self.parent = parent
                        self.event = threading.Event()
                
                    def run(self):
                        # started running, doing some stuff here
                
                        # waits for button click, i.e. when button is clicked, the event is set and then you may proceed
                        self.event.wait()
                        # NEVER REACHES HERE
                
                    def on_button_a_clicked(self):
                        print("reached here")        
                        self.event.set()
                
                class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
                
                    def __init__(self):
                        super(MainWindow, self).__init__()
                        self.setupUi(self)
                        self.setFixedSize(self.size())
                
                        self.start_button.clicked.connect(self.on_start_clicked)
                
                    def on_start_clicked(self):
                        self.thread = QThread()
                        self.worker = Procedure(self)
                        self.worker.moveToThread(self.thread)
                        self.thread.started.connect(self.worker.run)
                        self.button_a.clicked.connect(self.worker.on_button_a_clicked)
                        self.thread.start()
                
                def main():
                
                    app = QtWidgets.QApplication(sys.argv)
                    window = MainWindow()
                    window.show()
                    app.exec_()
                
                
                if __name__ == '__main__':
                    main()
                
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved