Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTime default initialization



  • Hey, curious about something (I'm new to Qt and am taking over someone else's code). I've noticed that they create a new QTime (static) object static QTime timer but they don't initialize it. Then, few lines down (bunch of functional calls), it does
    if (timer.isNull())) {timer.start(); //else if ... .
    I read about QTime default constructor
    0_1561678631546_49bef6c8-2bed-4a64-94bd-bea76b14af7e-image.png
    and so it looks like to me, the way this code was written declares all new variables at the top of the function as done by practice, then does a bunch of calls that actually do take up some execution time, and only then starts the timer by implementing if(timer.isNull()) {timer.start();} because it will be set to null by default since the person didn't initialize it. Is this correct?


  • Moderators

    @The178 said in QTime default initialization:

    it looks like to me, the way this code was written declares all new variables at the top of the function as done by practice, then does a bunch of calls that actually do take up some execution time, and only then starts the timer by implementing if(timer.isNull()) {timer.start();} because it will be set to null by default since the person didn't initialize it. Is this correct?

    Yep, you got it. See also https://doc.qt.io/qt-5/qtime.html#start

    Note: It took me a while to understand your post because the variable name is timer but the class is QTime. Just be aware that QTime != QTimer. See also QElapsedTimer for completeness: https://doc.qt.io/qt-5/qelapsedtimer.html



  • @JKSH Ah, gotcha! Thank you, yes I see that this would cause more trouble than needed, I'll look into renaming it.


Log in to reply