Impossible to call a method of another class with QTimer
-
wrote on 17 May 2022, 08:39 last edited by
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 -
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@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?
-
wrote on 17 May 2022, 08:55 last edited by
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/3