简体   繁体   English

QT GUI水平滑块setValue与QElapsedTimer

[英]QT GUI Horizontal Slider setValue with QElapsedTimer

I have a GUI Button that calls a shell script to start recording a video. 我有一个GUI按钮,该按钮调用外壳程序脚本开始录制视频。 In addition I want a slider to show progress of recording (maximum recording time is 30 seconds). 另外,我希望滑块显示录制进度(最长录制时间为30秒)。 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? 有人知道为什么吗?

Your main (UI) thread is busy iterating through your for loop and won't be able to handle the events to update your label. 您的主(UI)线程正忙于遍历for循环,因此无法处理事件以更新标签。

There is one quick and dirty solution (not tested), add the following line after setValue : 有一种快速有效的解决方案(未经测试),在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. 更好的解决方案是将流程处理移至单独的线程,并通过信号/插槽将进度通知主线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM