Home > Software engineering >  PyQt5 - QTime not changing values
PyQt5 - QTime not changing values

Time:10-23

I wanted to create a simple timer that gets shown to the screen and was using QTime to track the amount of time passed. However when I create a time with QTime(0, 0, 0) it will always stay at the initial value and will never change.

from PyQt5.QtCore import QTime

time = QTime(0, 0, 0)
print(time.toString("hh:mm:ss"))      # 00:00:00
    
time.addSecs(20)
print(time.toString("hh:mm:ss"))      # Still 00:00:00 for some reason

Why is the above code not updating the time variable and is there a simple fix for this?

CodePudding user response:

There is a super simple fix to this. time.addSecs(20) returns a value. So do time = time.addSecs(20) instead. That will fix your problem.

CodePudding user response:

From the documentation the function signature is...

QTime QTime::addSecs(int s) const

So you need...

time = time.addSecs(20)
print(time.toString("hh:mm:ss"))
  • Related