QTimer 在 Dialog 关闭后运行
QTimer runs after Dialog has been closed
我有一个 QDialog,我在其中创建了一个 QTimer 对象,它会触发每个 n秒一个函数。关闭对话框(点击 x 按钮)后,计时器仍在触发并且似乎没有被破坏。我怎样才能阻止它?目前作为一种解决方法,我在输入 closeEvent() 时明确调用 Qtimer.stop()?
我希望每个 class 成员都被删除,当 Window 关闭时,即使我显式调用 Deconstructor,Qtimer对象仍然存在。
from PyQt4 import QtGui, QtCore
import sys
def update():
print "tick..."
class Main(QtGui.QDialog):
def __init__(self, parent = None):
super(Main, self).__init__(parent)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(update)
self.timer.start(2000)
# scroll area
self.scrollArea = QtGui.QScrollArea()
self.scrollArea.setWidgetResizable(True)
# main layout
self.mainLayout = QtGui.QVBoxLayout()
self.setLayout(self.mainLayout)
def closeEvent(self, evt):
print "class event called"
self.timer.stop()
myWidget = Main()
myWidget.show()
http://doc.qt.io/qt-5/timers.html
The main API for the timer functionality is QTimer
. That class
provides regular timers that emit a signal when the timer fires, and
inherits QObject
so that it fits well into the ownership structure of
most GUI programs. The normal way of using it is like this:
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateCaption()));
timer->start(1000);
The QTimer
object is made into a child of this widget so that, when
this widget is deleted, the timer is deleted too. Next, its timeout()
signal is connected to the slot that will do the work, it is started
with a value of 1000 milliseconds, indicating that it will time out
every second.
在 C++ 中,计时器是小部件或另一个 QObject
的父级,然后它们的生命周期与 QObject
的生命周期相关联,但停止计时器仍然是一个好习惯当你不需要它的时候。当您调用 setLayout
时,布局会得到父级。定时器不知道它的父对象,所以它不会在小部件被销毁时被销毁。它只是位于堆上,仍然通过 QApplication
事件循环获得 运行。
http://doc.qt.io/qt-5/qobject.html#setParent
因此要么将 self 传递给 QTimer
的构造函数,要么在 QTimer
上调用 setParent
以将其正确设置到对象树中。
http://doc.qt.io/qt-5/objecttrees.html
更新:显然 setParent
在 PyQt 中不工作。只需在 QTimer
构造函数中传入 self
。
希望对您有所帮助。
我有一个 QDialog,我在其中创建了一个 QTimer 对象,它会触发每个 n秒一个函数。关闭对话框(点击 x 按钮)后,计时器仍在触发并且似乎没有被破坏。我怎样才能阻止它?目前作为一种解决方法,我在输入 closeEvent() 时明确调用 Qtimer.stop()?
我希望每个 class 成员都被删除,当 Window 关闭时,即使我显式调用 Deconstructor,Qtimer对象仍然存在。
from PyQt4 import QtGui, QtCore
import sys
def update():
print "tick..."
class Main(QtGui.QDialog):
def __init__(self, parent = None):
super(Main, self).__init__(parent)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(update)
self.timer.start(2000)
# scroll area
self.scrollArea = QtGui.QScrollArea()
self.scrollArea.setWidgetResizable(True)
# main layout
self.mainLayout = QtGui.QVBoxLayout()
self.setLayout(self.mainLayout)
def closeEvent(self, evt):
print "class event called"
self.timer.stop()
myWidget = Main()
myWidget.show()
http://doc.qt.io/qt-5/timers.html
The main API for the timer functionality is
QTimer
. That class provides regular timers that emit a signal when the timer fires, and inheritsQObject
so that it fits well into the ownership structure of most GUI programs. The normal way of using it is like this:QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(updateCaption())); timer->start(1000);
The
QTimer
object is made into a child of this widget so that, when this widget is deleted, the timer is deleted too. Next, itstimeout()
signal is connected to the slot that will do the work, it is started with a value of 1000 milliseconds, indicating that it will time out every second.
在 C++ 中,计时器是小部件或另一个 QObject
的父级,然后它们的生命周期与 QObject
的生命周期相关联,但停止计时器仍然是一个好习惯当你不需要它的时候。当您调用 setLayout
时,布局会得到父级。定时器不知道它的父对象,所以它不会在小部件被销毁时被销毁。它只是位于堆上,仍然通过 QApplication
事件循环获得 运行。
http://doc.qt.io/qt-5/qobject.html#setParent
因此要么将 self 传递给 QTimer
的构造函数,要么在 QTimer
上调用 setParent
以将其正确设置到对象树中。
http://doc.qt.io/qt-5/objecttrees.html
更新:显然 setParent
在 PyQt 中不工作。只需在 QTimer
构造函数中传入 self
。
希望对您有所帮助。