c++qtwidgetdelayblink

How to make a widget blink in Qt with a delay of one second?


I want to show and hide a widget with an interval of one second. My code is like:

if(ui->widget->isVisible())
        ui->widget->hide();
else
        ui->widget->show();

I need a one-second gap between showing and hiding my widget. I also want this to be repeated so that the widget starts blinking.


Solution

  • With QTimer it should be posible:

    QTimer *timer = new QTimer(this);
    constexpr int i = 1000; // 1s
    timer->setInterval(i);
    
    connect(timer, &QTimer::timeout, this, [this]{
        if (isVisible())
            hide();
        else
            show();
    });
    
    timer->start();