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. If condition to stay for a fixed time
Qt 6.11 is out! See what's new in the release blog

If condition to stay for a fixed time

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 2.2k 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.
  • R Offline
    R Offline
    russjohn834
    wrote on last edited by russjohn834
    #1

    Hi,

    I've a basic question. In the following code I'm switching the appearance (using StyleSheetOn and StyleSheetOff ) of a radio button based on IF condition.

    How do I make the StyleSheetOn appear for a fixed period of time , say 1 sec?

    if(a > 0.5)
    {
        ui->rdo_btn->show();
        ui->rdo_btn->setStyleSheet(StyleSheetOn);
    }
    else
    {
        ui->rdo_btn->setStyleSheet(StyleSheetOff);
    }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Use a QTimer, so example like this:

      if(a > 0.5)
      {
          ui->rdo_btn->show();
          ui->rdo_btn->setStyleSheet(StyleSheetOn);
      
          QTimer::singleShot(1000, this, [this]() { ui->rdo_btn->setStyleSheet(StyleSheetOff); });
      }
      else
      {
          ui->rdo_btn->setStyleSheet(StyleSheetOff);
      }
      

      (Z(:^

      R 1 Reply Last reply
      4
      • sierdzioS sierdzio

        Use a QTimer, so example like this:

        if(a > 0.5)
        {
            ui->rdo_btn->show();
            ui->rdo_btn->setStyleSheet(StyleSheetOn);
        
            QTimer::singleShot(1000, this, [this]() { ui->rdo_btn->setStyleSheet(StyleSheetOff); });
        }
        else
        {
            ui->rdo_btn->setStyleSheet(StyleSheetOff);
        }
        
        R Offline
        R Offline
        russjohn834
        wrote on last edited by
        #3

        @sierdzio Hi, I tried as you suggested, I get an error saying:

        mainwindow.cpp:348:87: error: variable 'StyleSheetOff' cannot be implicitly captured in a lambda with no capture-default specified
        mainwindow.cpp:319:13: note: 'StyleSheetOff' declared here
        mainwindow.cpp:348:40: note: lambda expression begins here
        
        jsulmJ 1 Reply Last reply
        0
        • R russjohn834

          @sierdzio Hi, I tried as you suggested, I get an error saying:

          mainwindow.cpp:348:87: error: variable 'StyleSheetOff' cannot be implicitly captured in a lambda with no capture-default specified
          mainwindow.cpp:319:13: note: 'StyleSheetOff' declared here
          mainwindow.cpp:348:40: note: lambda expression begins here
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @russjohn834

          QTimer::singleShot(1000, this, [this, StyleSheetOff]() { ui->rdo_btn->setStyleSheet(StyleSheetOff); });
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          5
          • R Offline
            R Offline
            russjohn834
            wrote on last edited by
            #5

            If I do this:

            if(a > 0.5)
            {
                ui->rdo_btn->show();
                ui->rdo_btn->setStyleSheet(StyleSheetOn);
            
                QTimer::singleShot(1000, this, [this, StyleSheetOff]() { ui->rdo_btn->setStyleSheet(StyleSheetOff); });
            }
            else
            {
                ui->rdo_btn->setStyleSheet(StyleSheetOff);
            }
            

            Apparently it behaves as before (it momentarily shows the StyleSheetOn and shows StyleSheetOff ).

            What I'm trying to do is , if the condition met (if(a > 0.5)) , radio button (rdo_btn) should show the StyleSheetOn for 1 sec and then StyleSheetOff.

            Could you please comment what went wrong here?

            sierdzioS 1 Reply Last reply
            0
            • R russjohn834

              If I do this:

              if(a > 0.5)
              {
                  ui->rdo_btn->show();
                  ui->rdo_btn->setStyleSheet(StyleSheetOn);
              
                  QTimer::singleShot(1000, this, [this, StyleSheetOff]() { ui->rdo_btn->setStyleSheet(StyleSheetOff); });
              }
              else
              {
                  ui->rdo_btn->setStyleSheet(StyleSheetOff);
              }
              

              Apparently it behaves as before (it momentarily shows the StyleSheetOn and shows StyleSheetOff ).

              What I'm trying to do is , if the condition met (if(a > 0.5)) , radio button (rdo_btn) should show the StyleSheetOn for 1 sec and then StyleSheetOff.

              Could you please comment what went wrong here?

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @russjohn834 said in If condition to stay for a fixed time:

              Could you please comment what went wrong here?

              The code here looks good. So the question is rather: how often is it called? Do you call this method many times per second, does a change often?

              (Z(:^

              1 Reply Last reply
              4
              • S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #7

                With the code that @sierdzio provided the radio button will stay on for at most 1 second. If a falls below 0.5 earlier than that it will turn the button off immediately. If you want something like exactly 1 second I would suggest introducing a boolean member variable which will be reset by the timer.

                Furthermore, I suggest that you use a single stylesheet for the two states specifying the different style for different toggle states. The right one will be picked according to the checked state of the radio button.

                Together, the code would look something like this:

                if(!keep_on) // don't change anything if the timer is still running (neither extend the "on" period nor toggle it off)
                {
                    if(a > 0.5)
                    {
                        ui->rd_btn->show();
                        ui->rd_btn->setChecked(true);
                
                        keep_on = true;
                        QTimer::singleShot(1000, this, [this]() { keep_on = false; ui->rdo_btn->setChecked(false); });
                    }
                    else
                    {
                        ui->rd_btn->setChecked(false);
                    }
                }
                

                You would also need to set the stylesheet once in the constructor, for example.

                Depending on what you really want, this might not be the right solution. If the on-state should be extended by another a > 0.5 while the timer is still running, you would need to adapt the code accordingly. If you want a <= 0.5 to immediatly switch to the off-state this needs changes as well.

                1 Reply Last reply
                2

                • Login

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