我有两个 QMainWindow 子类,MyWindow和MyTimerWindow。应用程序初始化并显示一个MyWindow实例,该实例除了初始化和显示一个MyTimerWindow实例之外什么都不做。MyTimerWindow创建一个子对象 QTimer,它每两秒触发printsomething函数。
当我通过单击标题栏中的 X手动关闭MyTimerWindow实例时, printsomething函数会每两秒执行一次。据我了解,当我关闭其父窗口时,应该销毁 QTimer 实例。有人可以解释为什么 QTimer 还活着吗?
这是代码:
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setWindowTitle("main window")
self.centralwidget = QtGui.QWidget()
self.setCentralWidget(self.centralwidget)
timerwindow = MyTimerWindow(self)
timerwindow.show()
class MyTimerWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyTimerWindow, self).__init__(parent)
self.setWindowTitle("timer window")
self.centralwidget = QtGui.QWidget()
self.setCentralWidget(self.centralwidget)
self.timer = QtCore.QTimer(self)
self.timer.setInterval(2000)
self.timer.timeout.connect(self.printsomething)
self.timer.start()
def printsomething(self):
print("something")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyWindow()
window.show()
returnvalue = app.exec_()
sys.exit(returnvalue)