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

Setting timeout for QLabel-Text without Signals&Sots



  • Hi,

    I am working on a GUI-Application and I have different Buttons. Under every Button I have a QLabel, which should show a Message for a certain time, e.g. 3 seconds.
    I do not want to use the status bar because it is not noticed at all or not immediately. I don't want to use a MessageBox either.
    So, I have 8 QLabels and I wrote a function for filling these Labels with a message:

    void MainWindow::setStatus(QString statusMessage, StatusWidget widget)
    {
    
        switch(statusWidget){
        case Data:
            ui->dataStatus->setText(statusMessage);
           /*set time out 3 seconds here */
          /* ui->dataStatus->text().clear()*/
            break;
    
        case File:
            ui->fileStatus->setText(statusMessage);
           /*set time out 3 seconds*/
          /*ui->dataStatus->text().clear()*/
            break;
    
        ....
         }
    }
    

    Usage:

    // When "ShowData" button is clicked..
    void MainWindow::on_showData_clicked(){
        setStatus("The data is being retrieved. Please wait..",Data)
    }
    

    So i hav read that it is not recommendet to use a Thread.sleep(), what would be a good alternative here without forcing the GUI to wait until the timeout is reached?



  • @TUStudi

    Setting timeout for QLabel-Text without Signals&Sots
    So i hav read that it is not recommendet to use a Thread.sleep(),

    That's because you should not use sleep() and you should use signals & slots. All you need to use here is a QTimer: set the text, set off the timer (singleShot() should do you), clear the text on timeout.



  • @TUStudi

    Setting timeout for QLabel-Text without Signals&Sots
    So i hav read that it is not recommendet to use a Thread.sleep(),

    That's because you should not use sleep() and you should use signals & slots. All you need to use here is a QTimer: set the text, set off the timer (singleShot() should do you), clear the text on timeout.



  • @JonB Thank you!


Log in to reply