Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

If condition to stay for a fixed time



  • 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);
    }
    

  • Moderators

    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);
    }
    


  • @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
    

  • Qt Champions 2019

    @russjohn834

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


  • 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?


  • Moderators

    @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?



  • 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.


Log in to reply