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. signal emit, but the slot function is not called.
Forum Updated to NodeBB v4.3 + New Features

signal emit, but the slot function is not called.

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 362 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.
  • K Offline
    K Offline
    kwon4308
    wrote on last edited by kwon4308
    #1

    signal emit, but the slot function is not called.
    Please tell me why.

    import sys
    
    from PySide6.QtCore import (
        QThread,
        QObject,
        Signal,
        Slot,
        Qt,
    )
    from PySide6.QtWidgets import QApplication
    from loguru import logger
    
    
    class Model(QObject):
        signalCommand = Signal(bool)
    
        @property
        def command(self) -> bool:
            return self._command
    
        @command.setter
        def command(self, value: bool) -> None:
            self._command = value
            self.signalCommand.emit(value)
            logger.info(f'model set {value}')
    
        def __init__(self):
            super().__init__()
    
            self._command = False
    
    
    class ControlThread(QThread):
        @property
        def isRunning(self) -> bool:
            return self.__threadRunning
    
        @isRunning.setter
        def isRunning(self, value: bool) -> None:
            self.__threadRunning = value
    
        def __init__(self, model: Model):
            super().__init__()
    
            self.isRunning = True
            self.__model = model
    
        def __del__(self):
            self.stop()
    
        def run(self) -> None:
            if not self.isRunning:
                self.isRunning = True
    
            while self.isRunning:
                self.msleep(1000)
    
                if self.__model.command:
                    self.__model.command = False
                else:
                    self.__model.command = True
    
        def stop(self) -> None:
            self.isRunning = False
    
    
    class Control(QObject):
        def __init__(self, model: Model):
            super().__init__()
    
            self.__model = model
    
            self.controlThread = ControlThread(self.__model)
            self.controlThread.start()
    
        def __del__(self):
            self.__stopThread()
    
        def __stopThread(self) -> None:
            self.controlThread.stop()
            self.controlThread.quit()
            self.controlThread.wait()
    
    
    class View(QObject):
        def __init__(self, model: Model, control: Control):
            super().__init__()
    
            self._model = model
            self._control = control
    
            self._model.signalCommand.connect(self.__command)
    
        @Slot(bool)
        def __command(self, value: bool):
            logger.info(f'__command {value}')
    
    
    class App(QApplication):
        def __init__(self, sys_argv):
            super(App, self).__init__(sys_argv)
    
            model_ = Model()
            control_ = Control(model_)
            view_ = View(model_, control_)
    
    
    if __name__ == '__main__':
        app = App(sys.argv)
        sys.exit(app.exec())
    
    
    JonBJ 1 Reply Last reply
    0
    • K kwon4308

      signal emit, but the slot function is not called.
      Please tell me why.

      import sys
      
      from PySide6.QtCore import (
          QThread,
          QObject,
          Signal,
          Slot,
          Qt,
      )
      from PySide6.QtWidgets import QApplication
      from loguru import logger
      
      
      class Model(QObject):
          signalCommand = Signal(bool)
      
          @property
          def command(self) -> bool:
              return self._command
      
          @command.setter
          def command(self, value: bool) -> None:
              self._command = value
              self.signalCommand.emit(value)
              logger.info(f'model set {value}')
      
          def __init__(self):
              super().__init__()
      
              self._command = False
      
      
      class ControlThread(QThread):
          @property
          def isRunning(self) -> bool:
              return self.__threadRunning
      
          @isRunning.setter
          def isRunning(self, value: bool) -> None:
              self.__threadRunning = value
      
          def __init__(self, model: Model):
              super().__init__()
      
              self.isRunning = True
              self.__model = model
      
          def __del__(self):
              self.stop()
      
          def run(self) -> None:
              if not self.isRunning:
                  self.isRunning = True
      
              while self.isRunning:
                  self.msleep(1000)
      
                  if self.__model.command:
                      self.__model.command = False
                  else:
                      self.__model.command = True
      
          def stop(self) -> None:
              self.isRunning = False
      
      
      class Control(QObject):
          def __init__(self, model: Model):
              super().__init__()
      
              self.__model = model
      
              self.controlThread = ControlThread(self.__model)
              self.controlThread.start()
      
          def __del__(self):
              self.__stopThread()
      
          def __stopThread(self) -> None:
              self.controlThread.stop()
              self.controlThread.quit()
              self.controlThread.wait()
      
      
      class View(QObject):
          def __init__(self, model: Model, control: Control):
              super().__init__()
      
              self._model = model
              self._control = control
      
              self._model.signalCommand.connect(self.__command)
      
          @Slot(bool)
          def __command(self, value: bool):
              logger.info(f'__command {value}')
      
      
      class App(QApplication):
          def __init__(self, sys_argv):
              super(App, self).__init__(sys_argv)
      
              model_ = Model()
              control_ = Control(model_)
              view_ = View(model_, control_)
      
      
      if __name__ == '__main__':
          app = App(sys.argv)
          sys.exit(app.exec())
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @kwon4308 said in signal emit, but the slot function is not called.:

          model_ = Model()
          control_ = Control(model_)
          view_ = View(model_, control_)
      

      How does anything work given that these are all local variables?

      K 1 Reply Last reply
      4
      • JonBJ JonB

        @kwon4308 said in signal emit, but the slot function is not called.:

            model_ = Model()
            control_ = Control(model_)
            view_ = View(model_, control_)
        

        How does anything work given that these are all local variables?

        K Offline
        K Offline
        kwon4308
        wrote on last edited by
        #3

        @JonB Wow, how did I not see that? Thank you so much. You're the best.

        1 Reply Last reply
        0
        • K kwon4308 has marked this topic as solved on

        • Login

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