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. Understanding setInterval(int msec) function

Understanding setInterval(int msec) function

Scheduled Pinned Locked Moved Solved General and Desktop
intervalqtimer
5 Posts 2 Posters 2.5k 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 25 Jun 2019, 22:54 last edited by The178
    #1

    Hi, this might seem straightforward but I can't exactly understand from the documentation
    0_1561503102067_63c3df7a-91b5-4f03-9125-946d40fa1f79-image.png
    I understand this means that we are setting the timeout interval to be x amount of milliseconds. I don't know what that means in practice. What timeout is it going to look for, in what sense, and what will be the effect of the time out? I've never used Qt before and it's been a slow learning curve getting familiar with all the widgets etc. Thank you

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 25 Jun 2019, 23:15 last edited by
      #2

      the purpose of the timer is to execute some method/function at the time it expires or times out. there are singlshot timers that countdown once, and there are repetitive timers that countdown over and over again each time they expire. the interval is just that, the time interval between timeouts or the timer expiring. The reason you might use an interval of zero is to immediately (almost immediately) execute the timeout function to do some initialization, and you would then set a non-zero interval from within the timeout function.

      T 1 Reply Last reply 25 Jun 2019, 23:32
      4
      • K Kent-Dorfman
        25 Jun 2019, 23:15

        the purpose of the timer is to execute some method/function at the time it expires or times out. there are singlshot timers that countdown once, and there are repetitive timers that countdown over and over again each time they expire. the interval is just that, the time interval between timeouts or the timer expiring. The reason you might use an interval of zero is to immediately (almost immediately) execute the timeout function to do some initialization, and you would then set a non-zero interval from within the timeout function.

        T Offline
        T Offline
        The178
        wrote on 25 Jun 2019, 23:32 last edited by
        #3

        @Kent-Dorfman Ah, I see, I found some more information about implementing repetitive QTimers.
        The code I am trying to understand is mainwindow class. It has a private variable QTimer *timer1 and it is initialized in the constructor of MainWindow with the following lines:

        timer1 = new QTimer(this);					
        	timer1->setTimerType(Qt::PreciseTimer); // [ms] accuracy
        	timer1->setInterval(500); /
        	connect(timer1, SIGNAL(timeout(void)), this, SLOT(1Timer(void)));
        	timer1->start();
        

        1Timer(void) slot function is defined elsewhere.
        So, the section of code I wrote above is saying that every 500ms, 1Timer(void) will be executed. Since this is a private variable which is initialized inside the constructor of MainWindow and MainWindow object is created only once and called only once from the main, does this mean that as long as the MainWindow gui is running, it will execute 1Timer(void) every half a second? I'd think the SIGNAL(timeout(void)) is the signal created by object timer1 which triggers the SLOT of this object to execute 1Timer(void). It seems like these types of arguments are somewhat standard since I found some documentation on it, but I'd like to understand how this works in more detail if you have a second to explain.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kent-Dorfman
          wrote on 26 Jun 2019, 02:27 last edited by
          #4

          Your understanding is correct. However, I believe your slot cannot exist as you name it "1Timer" which would be an invalid function name since it starts with a digit. Otherwise it looks ok.

          T 1 Reply Last reply 28 Jun 2019, 20:52
          4
          • K Kent-Dorfman
            26 Jun 2019, 02:27

            Your understanding is correct. However, I believe your slot cannot exist as you name it "1Timer" which would be an invalid function name since it starts with a digit. Otherwise it looks ok.

            T Offline
            T Offline
            The178
            wrote on 28 Jun 2019, 20:52 last edited by
            #5

            @Kent-Dorfman that was my bad, the actual name in the code is different (I didn't want to use the original since it could potentially disclose information I am not allowed to share) and I wasn't creative enough to think of something so I said timer1 haha. The actual variable is all letters :).

            1 Reply Last reply
            0

            1/5

            25 Jun 2019, 22:54

            • Login

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