This forum has been archived. All content is frozen. Please use KDE Discuss instead.

[SOLVED] PyQt timer woes

Tags: None
(comma "," separated)
User avatar
JontheEchinda
KDE Developer
Posts
309
Karma
4
OS

[SOLVED] PyQt timer woes

Sat Dec 20, 2008 6:55 pm
Hello guys.
I've been having a bit of a hard time getting Timers to work properly with my PyQt4 apps. I thought maybe you guys could tell me what I am doing wrong.

The problem is that the updateTimer function gets called twice per second the second time you start the timer. The third time you start the timer updateTimer gets called 3 times on each timeout, and so on. I have no clue why this is happening.

Here's an example of what I'm trying to do:
Code: Select all
# -*- coding: utf-8 -*-
import sys
import os.path

from PyQt4 import QtCore, QtGui, uic

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.ui = uic.loadUi('timerexample.ui')

        QtCore.QObject.connect(self.ui.startButton,QtCore.SIGNAL("clicked()"), self.startTimer)
        QtCore.QObject.connect(self.ui.stopButton,QtCore.SIGNAL("clicked()"), self.stopTimer)

        self.myTimer = QtCore.QTimer(self)
        self.timerTime = 0

        self.setCentralWidget(self.ui)

    def startTimer(self):
        self.myTimer.start(1000)
        QtCore.QObject.connect(self.myTimer,QtCore.SIGNAL("timeout()"), self.updateTimer)
           
    def stopTimer(self):
        self.myTimer.stop()

    def updateTimer(self):
        print ('The timer sez to go')
        totalSeconds = self.timerTime
        totalSeconds = totalSeconds + 1
        self.timerTime = self.timerTime + 1

        finalMinutes = totalSeconds / 60
        finalSeconds = totalSeconds % 60

        finalTime = QtCore.QTime(0, finalMinutes, finalSeconds)
        self.ui.timeEdit.setTime(finalTime)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())


And here's timerexample.ui. Note that you will have to resize the window once you start the app because I was too lazy to fix the mainwindow's minimum size. =P

Code: Select all
 timerexample
 
 
   
    0
    0
    303
    161
   
 
 
   MainWindow
 
 
   
   
     0
     0
     303
     161
   
   
   
   
     
      10
      110
      118
      33
     
   
   
     start
   
   
   
   
     
      150
      110
      118
      33
     
   
   
     stop
   
   
   
   
     
      20
      20
      118
      33
     
   
   
     mm:ss
   
   
 
 
 
 



Any help is appreciated!

Last edited by JontheEchinda on Sun Dec 21, 2008 3:58 am, edited 1 time in total.


JontheEchinda, proud to be a member of the Kubuntu team since July 2008.
Image
Image
User avatar
anda_skoa
KDE Developer
Posts
783
Karma
4
OS

RE: PyQt timer woes

Sat Dec 20, 2008 11:52 pm
You connect to the signal every time you start the timer, ending up with one additional connection for every start.

Just connect once, e.g. in the constructor after creating the timer

Cheers,
_


anda_skoa, proud to be a member of KDE forums since 2008-Oct.
User avatar
JontheEchinda
KDE Developer
Posts
309
Karma
4
OS

RE: PyQt timer woes

Sun Dec 21, 2008 3:57 am
Aaaah! It's so obvious now. Thanks a lot.


JontheEchinda, proud to be a member of the Kubuntu team since July 2008.
Image
Image


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell