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. How to implement a time delay
Qt 6.11 is out! See what's new in the release blog

How to implement a time delay

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 941 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.
  • S Offline
    S Offline
    SruDom
    wrote on last edited by
    #1

    Hi,

    I've real-time signal, I need to check if the signal crosses a threshold and set a delay for about 1 second. How do I implement that?

    if(a_vertical> onThreshold) //what's the expression to set a time delay?
        {
            ui->rdo_btn_vertical->setStyleSheet(StyleSheetOn1); // On  LED
        }
        else
        {
            ui->rdo_btn_vertical->setStyleSheet(StyleSheetOff1); // Off LED
        }
    
    ODБOïO 1 Reply Last reply
    0
    • S SruDom

      Hi,

      I've real-time signal, I need to check if the signal crosses a threshold and set a delay for about 1 second. How do I implement that?

      if(a_vertical> onThreshold) //what's the expression to set a time delay?
          {
              ui->rdo_btn_vertical->setStyleSheet(StyleSheetOn1); // On  LED
          }
          else
          {
              ui->rdo_btn_vertical->setStyleSheet(StyleSheetOff1); // Off LED
          }
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @SruDom hi, you can use QTimer

      1 Reply Last reply
      3
      • S Offline
        S Offline
        SruDom
        wrote on last edited by
        #3

        @LeLev Hi, could you show me an example to set delay using QTimer?

        ODБOïO 1 Reply Last reply
        0
        • S SruDom

          @LeLev Hi, could you show me an example to set delay using QTimer?

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #4

          @SruDom there are examples in the page i linked

          1 Reply Last reply
          3
          • S Offline
            S Offline
            SruDom
            wrote on last edited by SruDom
            #5

            I did something like this, and it works
            start QTimer in single shot mode and check if it's running (should not be) while checking the threshold:

            if(a_vertical> onThreshold && !timer.isActive()) 
                {
                    emit startTimer();
            
                    ui->rdo_btn_vertical->setStyleSheet(StyleSheetOn1); // On  LED
                }
                else
                {
                    ui->rdo_btn_vertical->setStyleSheet(StyleSheetOff1); // Off LED
                }
            
            
            void MainWindow::startTimer()
            {
                 timer.setSingleShot(true);
                 timer.start(500); // 500ms
            
            }
            
            1 Reply Last reply
            1
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by SimonSchroeder
              #6

              @SruDom said in How to implement a time delay:

              I did something like this, and it works

              Your solution is a little hacky, as this is not how timers are intended to be used. What your code actually does is that the LED will be turned on immediately for a fraction of a second and then immediately turned off again.

              What you should do is connect the timeout() signal of the QTimer to your own slot which then turns the LED on. This would also eliminate the check for timer.isActive() which I would replace with another boolean to not restart the timer while the LED is already on.

              BTW, the radio button has a checked state and you can specify a single stylesheet which distinguishes between the checked and unchecked state. The stylesheet would need to be only set once. This is what your code would then look like (together with the suggestion I made above):

              if(a_vertical > onThreshold)
              {
                  if(!ui->rdo_btn_vertical->isChecked())
                      emit startTime();
              }
              else
              {
                  ui->rdo_btn_vertical->setChecked(false);
              }
              

              Inside your timeout slot you then call ui->rdo_btn_vertical->setChecked(true).

              1 Reply Last reply
              0

              • Login

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