Home > front end >  How to create timed transition in Qt?
How to create timed transition in Qt?

Time:12-11

I am trying to create a timed transition in Qt (C ) and this is driving me nuts. I have three "LEDs" that are illuminated on the UI. When the user clicks a button, I want them to die sequentially, like this but in reverse:

I have this function that is called when the button is pressed (this is the slot):

void MainWindow::turnOff(int i){
    while(i>0){
        QTest::qSleep(250);
        changeBrightness(i);
        qInfo("\n\n\at easeOff %d", brightness);
        i--;
    }
}

and the changeBrightness function:

void MainWindow::changeBrightness(int i){
    if(i > 0 && i <= 20){
        ui->LED1->setStyleSheet("QLabel { color: black; }");
        ...
    }
    brightness = i;
}

The console shows the value changing, but the actual UI transition only happens at the ends (all the LEDs go off at once). Brightness is a value between 1 and 100. Any help is appreciated.

CodePudding user response:

@sophryshko's suggestion of the repaint() function worked!

void MainWindow::turnOff(int i){
    while(i>0){
        QTest::qSleep(250); 
        ui->LED1->repaint();
        changeBrightness(i);
        qInfo("\n\n\at easeOff %d", brightness);
        i--;
    }
}

Thank you for the help. Will add the QTimer object as well but this was sufficient to change the UI behavior.

  •  Tags:  
  • c qt
  • Related