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. Detecting whether a singleshot QTimer is currently being run
Forum Updated to NodeBB v4.3 + New Features

Detecting whether a singleshot QTimer is currently being run

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.7k Views 2 Watching
  • 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    Hi. I have a function that starts a singleshot QTimer for 5 seconds. During those 5 seconds, there are a series of functions that I want to behave differently.

    The way I have it executed right now is like so:

    bool isSingleShotActive;
    void functionName(){
       //Some other function processes
    
            QTimer::singleShot(5000, this, SLOT(otherFunctionName())); //triggers the determineStability function after 5 seconds.
            isSingleShotActive = true;
    }
    
    void otherFunctionName(){
       //Some other function processes
            isSingleShotActive = false;
    }
    
    void evenOtherFunctionName(){
       if (isSingleShotActive == false)
         do_the_special_thing;
        else
          do_the_default_thing;
    }
    

    I was wondering whether there is a signal I can use on the oneshot to indicate that the oneshot is active.

    1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      @mchinand Are you suggesting building a QTimer and changing singleShot to true instead of using QTimer::singleShot? My understanding of QTimer::singleShot is that it's just a function and not a QTimer object, is that correct?

      Something like this:

          QTimer *timer = new QTimer(this);
          timer->setSingleShot(true);
          connect(timer, &QTimer::timeout, this, SLOT(otherFunctionName()));
          timer->start(5000);
      

      In that case will I have to declare QTimer *timer outside of the function, given that I need it's existence to be detected by another function?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @Dummie1138
      static QTimer::singleShot() is really just a "convenience" function. It simply creates/uses a QTimer instance of its own, which you cannot see/access.

      Yes, the code you show is indeed the correct/corresponding way to create your own QTimer instance and set it to single-shot mode. If you want to access that QTimer variable from elsewhere you will need to e.g. make it a class member variable, and an accessor method if you want it accessible outside the class.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mchinand
        wrote on last edited by
        #2

        You could create your instance of QTimer on the heap and then use QTimer::isActive() in your other method.

        Dummie1138D 1 Reply Last reply
        0
        • M mchinand

          You could create your instance of QTimer on the heap and then use QTimer::isActive() in your other method.

          Dummie1138D Offline
          Dummie1138D Offline
          Dummie1138
          wrote on last edited by Dummie1138
          #3

          @mchinand Are you suggesting building a QTimer and changing singleShot to true instead of using QTimer::singleShot? My understanding of QTimer::singleShot is that it's just a function and not a QTimer object, is that correct?

          Something like this:

              QTimer *timer = new QTimer(this);
              timer->setSingleShot(true);
              connect(timer, &QTimer::timeout, this, SLOT(otherFunctionName()));
              timer->start(5000);
          

          In that case will I have to declare QTimer *timer outside of the function, given that I need it's existence to be detected by another function?

          JonBJ 1 Reply Last reply
          0
          • Dummie1138D Dummie1138

            @mchinand Are you suggesting building a QTimer and changing singleShot to true instead of using QTimer::singleShot? My understanding of QTimer::singleShot is that it's just a function and not a QTimer object, is that correct?

            Something like this:

                QTimer *timer = new QTimer(this);
                timer->setSingleShot(true);
                connect(timer, &QTimer::timeout, this, SLOT(otherFunctionName()));
                timer->start(5000);
            

            In that case will I have to declare QTimer *timer outside of the function, given that I need it's existence to be detected by another function?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Dummie1138
            static QTimer::singleShot() is really just a "convenience" function. It simply creates/uses a QTimer instance of its own, which you cannot see/access.

            Yes, the code you show is indeed the correct/corresponding way to create your own QTimer instance and set it to single-shot mode. If you want to access that QTimer variable from elsewhere you will need to e.g. make it a class member variable, and an accessor method if you want it accessible outside the class.

            1 Reply Last reply
            3

            • Login

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