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. Impossible to call a method of another class with QTimer
QtWS25 Last Chance

Impossible to call a method of another class with QTimer

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 540 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.
  • T Offline
    T Offline
    Todakouma
    wrote on last edited by
    #1

    Hello,

    I have a problem with QTimer, I have in my code 2 classes. The class A calls a method of the class B containing a QTimer but this one doesn't work. However I don't receive any error from python and my application launches without any problem except the QTimer which doesn't work.

    Here is my code:

    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import QApplication
    import sys
    from PyQt5.QtCore import QTimer
    import visuui
    
    class MyClass:
        def __init__(self):
            print("init")
            self.timer = QTimer()
            self.timer.timeout.connect(self.myMethod)
    
        def myMethod(self):
            print("x")
    
        def start(self):
            print("start")
            self.timer.start(300)
    
        def stop(self):
            print("stop")
            self.timer.stop()
    
    
    class AppVisu(QtWidgets.QMainWindow, visuui.Ui_MainWindow):
        def __init__(self, parent=None):
            super(AppVisu, self).__init__(parent)
            self.setupUi(self)
    
            myclass = MyClass()
            myclass.start()
    
    def main():
        app = QApplication(sys.argv)
        form = AppVisu()
        form.show()
        app.exec_()
    
    if __name__ == '__main__':
        main()
    

    I use QtDesigner that's why I import the visuui file.
    As you can see the code is simple, you can try it on your side to see if the QTimer works but for me it doesn't work.
    If you have any solutions to my problem I'm interested !

    Thank you in advance :D
    Todakouma

    jsulmJ 1 Reply Last reply
    0
    • T Todakouma

      Hello,

      I have a problem with QTimer, I have in my code 2 classes. The class A calls a method of the class B containing a QTimer but this one doesn't work. However I don't receive any error from python and my application launches without any problem except the QTimer which doesn't work.

      Here is my code:

      from PyQt5 import QtWidgets
      from PyQt5.QtWidgets import QApplication
      import sys
      from PyQt5.QtCore import QTimer
      import visuui
      
      class MyClass:
          def __init__(self):
              print("init")
              self.timer = QTimer()
              self.timer.timeout.connect(self.myMethod)
      
          def myMethod(self):
              print("x")
      
          def start(self):
              print("start")
              self.timer.start(300)
      
          def stop(self):
              print("stop")
              self.timer.stop()
      
      
      class AppVisu(QtWidgets.QMainWindow, visuui.Ui_MainWindow):
          def __init__(self, parent=None):
              super(AppVisu, self).__init__(parent)
              self.setupUi(self)
      
              myclass = MyClass()
              myclass.start()
      
      def main():
          app = QApplication(sys.argv)
          form = AppVisu()
          form.show()
          app.exec_()
      
      if __name__ == '__main__':
          main()
      

      I use QtDesigner that's why I import the visuui file.
      As you can see the code is simple, you can try it on your side to see if the QTimer works but for me it doesn't work.
      If you have any solutions to my problem I'm interested !

      Thank you in advance :D
      Todakouma

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

      @Todakouma said in Impossible to call a method of another class with QTimer:

      myclass = MyClass()

      Do you know what the lifetime of myclass variable is?

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

      1 Reply Last reply
      2
      • T Offline
        T Offline
        Todakouma
        wrote on last edited by
        #3

        OMG, I just understand, it's a stupid mistake. I initialized my class in the init method...
        Now it works:

        class AppVisu(QtWidgets.QMainWindow, visuui.Ui_MainWindow):
            myclass = MyClass()
        
            def __init__(self, parent=None):
                super(AppVisu, self).__init__(parent)
                self.setupUi(self)
        
                self.myclass.start()
        

        Thank you very much !

        1 Reply Last reply
        2

        • Login

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