简体   繁体   English

在仍然进行非GUI工作的同时,如何以预定间隔更新GUI?

[英]How to update the GUI at a pre-determined interval, while still doing non-GUI work?

Basically, I'm doing some image processing and using a QImage in a QLabel to display the current frame in a video sequence. 基本上,我正在做一些图像处理,并在QLabel中使用QImage来显示视频序列中的当前帧。 Let's say I want to update the QImage to the next frame at 30 fps (or if some processing is not done by the 30 fps interval, wait until it's done), but I don't want the whole program to stop during that 30 fps. 假设我想以30 fps的速度将QImage更新到下一帧(或者如果在30 fps的间隔内未完成某些处理,请等到完成为止),但是我不希望整个程序在30 fps的过程中停止。

So the flow is... 所以流程是...

if (done_some_work && 30fps_interval_has_passed)
{
   updateQImage();
}

How is this done in Qt? 如何在Qt中完成? Thanks! 谢谢!

And use a QTimer to send a signal repaint the frame every 1000/30 ms 并使用QTimer发送信号每1000/30毫秒重新绘制一帧

myTimer= new QTimer(this);
myTimer->setInterval(1000/fps); // ms
connect(myTimer, SIGNAL(timeout()), this, SLOT(doNextFrame())); 

// where
public slots:
    virtual void doNextFrame() {repaint();}

The comment is correct, you should do your work in another thread and then signal that you want to update the UI thread when you want to update something visually. 注释是正确的,您应该在另一个线程中进行工作,然后在要进行视觉更新时发出要更新UI线程的信号。 Don't do processing work on the UI thread or your GUI will be unresponsive. 不要在UI线程上进行处理工作,否则您的GUI将无响应。 This isn't so much a QT question as more of a GUI question. 与其说是QT问题,还不如说是GUI问题。

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

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