Important: Please read the Qt Code of Conduct -

Qtimer. A little assistance

  • Hello

    Some demo code for Qtimer facility that I have scoured Google for around 2 hours now is driving me mad.
    They label the page 'Qtimer tutorial' and it always seems to have some vital bit of information missing. Hence, it doesn't compile.

    All I am trying to achieve is calling a routine once a second.

    Anyone got a link to a decent Python 3.8 tutorial that an idiot can follow?

  • Lifetime Qt Champion


    What error do you have ?
    Can you show the code you are using ?
    It will be easier to spot the issue.

  • @Stevolution
    Glancing through the code in looks simple and workable. Did you try this one? I'm hoping you can copy & paste and it will just work. If you are PySide2, replacing all PyQt5 by PySide2 looks to me will work.

  • Banned

    Here is a slightly cleaned up version (problematic code has been replaced with solid code) of that code that JonB shared -- it also handles the if you use PyQt5 versus PySide2

        from PyQt5.QtCore    import QTimer, QTime, Qt, pyqtSlot
        from PyQt5.QtGui     import QFont
        from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
    except ImportError:
        from PySide2.QtCore    import QTimer, QTime, Qt, Slot as pyqtSlot
        from PySide2.QtGui     import QFont
        from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
    class AppDemo(QWidget):
        def __init__(self):
            self.resize(250, 100)
         #  Not a  problem but I find explicit is easier to understand and maintain
            Font = QFont()
            Font.setFamily('Open Sans')
            self.lblClock = QLabel()
            VBox = QVBoxLayout()
            tmrClock = QTimer(self)
            tmrClock .timeout.connect(self.TickTock)
            tmrClock .start(1000) # update every second
          # This Primes the View 
        def TickTock(self):
            CurTime = QTime.currentTime()
            TimeDsply = CurTime.toString('hh:mm:ss')
    if __name__ == "__main__":
        MainEvntThred = QApplication([])
        MainApp = AppDemo()

Log in to reply