Search code examples
c++raspberry-piqtguiqtonpi

QT GUI Horizontal Slider setValue with QElapsedTimer


I have a GUI Button that calls a shell script to start recording a video. In addition I want a slider to show progress of recording (maximum recording time is 30 seconds). I build the following function:

coid MainWindow::on_recordStart_clicked()
{
QElapsedTimer timer;
QProcess *Prozess = new QProcess();
Prozess->start("record.sh");
timer.start();
for(;;)
{
ui->timelineLabel->setText(QString::number(timer.elapsed())); //label
ui->timeLine->setValue(timer.elapsed());  //slider
if (timer.hasExpired(30000)) break;
}

It only updates the Slider and Label after the break. Anyone knows why?


Solution

  • Your main (UI) thread is busy iterating through your for loop and won't be able to handle the events to update your label.

    There is one quick and dirty solution (not tested), add the following line after setValue:

    QCoreApplication::processEvents();
    

    The nicer solution would to move the process handling to a separate thread and notify the main thread about the progress via a signal/slot.