Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTime default initialization
Forum Update on Monday, May 27th 2025

QTime default initialization

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 698 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    The178
    wrote on 27 Jun 2019, 23:38 last edited by
    #1

    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?

    J 1 Reply Last reply 28 Jun 2019, 01:55
    0
    • T The178
      27 Jun 2019, 23:38

      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?

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 28 Jun 2019, 01:55 last edited by JKSH
      #2

      @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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      T 1 Reply Last reply 28 Jun 2019, 18:51
      5
      • J JKSH
        28 Jun 2019, 01:55

        @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

        T Offline
        T Offline
        The178
        wrote on 28 Jun 2019, 18:51 last edited by
        #3

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

        1 Reply Last reply
        0

        1/3

        27 Jun 2019, 23:38

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved