在Qt中使用定時(shí)器有兩種方法穿扳,一種是使用QObiect類的定時(shí)器;一種是使用QTimer類。
1.QObject類的定時(shí)器
通過(guò)QObject.startTimer()翩腐,可以把一個(gè)一毫秒為單位的時(shí)間間隔作為參數(shù)來(lái)開始定時(shí)器,這個(gè)函數(shù)返回一個(gè)唯一的整數(shù)定時(shí)器的標(biāo)識(shí)符膏燃。這個(gè)定時(shí)器開始就會(huì)在每一個(gè)時(shí)間間隔"觸發(fā)"茂卦,直到明確的使用這個(gè)定時(shí)器的標(biāo)識(shí)符來(lái)調(diào)用QObject.killTimer()結(jié)束。
當(dāng)定時(shí)器觸發(fā)時(shí)组哩,應(yīng)用程序會(huì)發(fā)送一個(gè)QTimerEvent()事件等龙。在事件循環(huán)中处渣,處理器按照事件隊(duì)列的順序來(lái)處理定時(shí)器事件。當(dāng)處理器正忙于其它事件處理時(shí)蛛砰,定時(shí)器就不能立即處理罐栈。
startTimer的格式為:
int QObject.startTimer (self, int interval[,Qt.TimerType timerType = Qt.CoarseTimer])
開始一個(gè)定時(shí)器并返回定時(shí)器ID,在中止定時(shí)器時(shí)要用到。如果不能開始一個(gè)定時(shí)器泥畅,將返回0荠诬。定時(shí)器開始后,每隔interval毫秒間隔將觸發(fā)一次超時(shí)事件位仁,直到killTimer()被調(diào)用來(lái)刪除定時(shí)器柑贞。如果interval為0,那么定時(shí)器事件在沒(méi)有窗口系統(tǒng)事件需要處理時(shí)都會(huì)發(fā)生聂抢。
Qt.TimerType可以有三個(gè)值:
Qt.PreciseTimer 0 精確定時(shí)器:盡可能保持毫秒準(zhǔn)確
Qt.CoarseTimer 1 粗定時(shí)器:5%的誤差間隔
Qt.VeryCoarseTimer 2 很粗的定時(shí)器:只能到秒級(jí)
timerEvent的格式為:
timerEvent(self,QTimerEvent qEvent)
通過(guò)QTimerEvent的timerId()可以取得定時(shí)器的ID钧嘶。
killTimer的格式:
killTimer(self,int Id)
中止通過(guò)startTimer獲得的Id定時(shí)器。
下面的代碼為在窗口中創(chuàng)建一個(gè)顯示當(dāng)前系統(tǒng)時(shí)間的時(shí)鐘琳疏,并有時(shí)鐘啟動(dòng)和停止的按鈕有决。
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtWidgets
import time
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.setWindowTitle("窗口中的時(shí)鐘")
self.resize(200, 100)
self.timer_id = 0
self.label = QtWidgets.QLabel("")
self.label.setAlignment(QtCore.Qt.AlignHCenter)
self.button1 = QtWidgets.QPushButton("開始")
self.button2 = QtWidgets.QPushButton("停止")
self.button2.setEnabled(False)
vbox = QtWidgets.QVBoxLayout()
vbox.addWidget(self.label)
vbox.addWidget(self.button1)
vbox.addWidget(self.button2)
self.setLayout(vbox)
self.button1.clicked.connect(self.on_clicked_button1)
self.button2.clicked.connect(self.on_clicked_button2)
def on_clicked_button1(self):
self.timer_id = self.startTimer(1000, timerType = QtCore.Qt.VeryCoarseTimer)
self.button1.setEnabled(False)
self.button2.setEnabled(True)
def on_clicked_button2(self):
if self.timer_id:
self.killTimer(self.timer_id)
self.timer_id = 0
self.button1.setEnabled(True)
self.button2.setEnabled(False)
def timerEvent(self, event):
self.label.setText(time.strftime("%H:%M:%S"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
除了使用QObject的startTimer()外,還可以使用QtCore模塊中的QTimer類空盼。
QTimer的構(gòu)造函數(shù)為:QTimer([parent=None])
方法有:
setInterval(self,int msec) 定時(shí)器觸發(fā)的時(shí)間間隔毫秒數(shù)书幕。如果interval為0,那么定時(shí)器事件在沒(méi)有窗口系統(tǒng)事件需要處理時(shí)都會(huì)發(fā)生揽趾。
startTimer(self.[int msec]) 起動(dòng)定時(shí)器按咒。
stop(self) 停止定時(shí)器
isActive(self) 如果定時(shí)器在運(yùn)行,返回True但骨,否則励七,返回False。
timerId(self) 如果定時(shí)器在運(yùn)行奔缠,返回定時(shí)器的ID掠抬,否則,返回-1校哎。
interval(self) 返回setInterval()的設(shè)定值两波。
setSingleShot(self,bool asingleShot) 如果為True,定時(shí)器只工作一次闷哆;否則腰奋,重復(fù)工作。
setTimerType(self,Qt.TimerType timerType) 指定定時(shí)器的類型抱怔,參數(shù)可參照startTimer()劣坊。
timerType() 返回setTimerType()的設(shè)定值。
下列是用QTimer實(shí)現(xiàn)前面例子功能的代碼:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtWidgets
import time
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.setWindowTitle("窗口中的時(shí)鐘")
self.resize(200, 100)
self.label = QtWidgets.QLabel("")
self.label.setAlignment(QtCore.Qt.AlignHCenter)
self.button1 = QtWidgets.QPushButton("開始")
self.button2 = QtWidgets.QPushButton("結(jié)束")
self.button2.setEnabled(False)
vbox = QtWidgets.QVBoxLayout()
vbox.addWidget(self.label)
vbox.addWidget(self.button1)
vbox.addWidget(self.button2)
self.setLayout(vbox)
self.button1.clicked.connect(self.on_clicked_button1)
self.button2.clicked.connect(self.on_clicked_button2)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.on_timeout);
def on_clicked_button1(self):
self.timer.start(1000) # 1 секунда
self.button1.setEnabled(False)
self.button2.setEnabled(True)
def on_clicked_button2(self):
self.timer.stop()
self.button1.setEnabled(True)
self.button2.setEnabled(False)
def on_timeout(self):
self.label.setText(time.strftime("%H:%M:%S"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())