2016-07-06 136 views
1

我有一個pySide應用程序,它使用QTimer每分鐘刷新其狀態一次。在某些情況下,我需要強制立即更新,然後重新啓動定時器在PySide中強制QTimer()超時

self.timer = QTimer() 
self.timer.timeout.connect(self._update_status) 
self.timer.start(60 * 1000) 

有沒有什麼辦法來迫使計時器到期併發出超時信號?

回答

1

最乾淨的解決方案似乎簡單:

 self.timer.start() # restart the timer 
     self.timer.timeout.emit() # force an immediate update 

也可以通過調用setInterval(1)強制立即更新,但是這有一個缺點,你將無需再次復位定時器間隔在連接到信號時隙:

 self.timer.setInterval(1) # force an immediate update 

    def _update_status(self): 
     ... 
     if self.timer.interval() == 1: 
      self.timer.setInterval(60 * 1000) # reset the interval 

(請注意,如果使用的的間隔,QT將僅發射Timeo酒店一旦事件隊列被清除,就會立即發出信號。因此,嚴格來說,setInterval(0)不一定會強制立即更新)。

+0

你的第一個建議使用timeout.emit()工作得很好,很簡單。 TNX。 – k3it