使用 QFuture 更新 QProgressDialog

发布于 2024-08-14 01:15:32 字数 478 浏览 3 评论 0原文

主 GUI 线程在等待 QFuture 时更新 QProgressDialog 的正确方法是什么?具体来说,这个循环中发生了什么:

QProgressDialog pd(...);
QFuture f = ...;

while (!f.isFinished()) {
  pd.setValue(f.progressValue());

  // what goes here?
}

现在我有一个类似于 sleep() 的调用,但这不是最佳的(当然会引入一些 GUI 延迟)。

如果我什么都不放,主线程将循环 pd.setValue(),浪费 CPU 周期。

我希望放置类似 QCoreApplication::processEvents(flags,maxtime) 的内容,但如果事件队列为空,它会立即返回。我想要一个即使队列为空也能等到 maxtime 或其他时间的版本。这样,我就得到了延迟,并且主线程始终准备好响应 GUI 事件。

What's the proper way for the main GUI thread to update a QProgressDialog while waiting for a QFuture. Specifically, what goes in this loop:

QProgressDialog pd(...);
QFuture f = ...;

while (!f.isFinished()) {
  pd.setValue(f.progressValue());

  // what goes here?
}

Right now I have a sleep() like call there, but that's not optimal (and ofcourse introduces some GUI latency).

If I put nothing, the main thread will loop-pole pd.setValue(), wasting CPU cycles.

I was hoping of putting something like QCoreApplication::processEvents(flags,maxtime), but that returns immediately if the event queue is empty. I'd like a version that waits until maxtime or whatever even if the queue is empty. That way, I get my delay and the main thread is always ready to respond to GUI events.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2024-08-21 01:15:32

使用 QFutureWatcher 使用信号和槽监视 QFuture 对象。

QFutureWatcher watcher;
QProgressDialog pd(...);
connect(&watcher, SIGNAL(progressValueChanged(int)), &pd, SLOT(setValue(int)));
QFuture f = ...
watcher.setFuture(f);

Use a QFutureWatcher to monitor the QFuture object using signals and slots.

QFutureWatcher watcher;
QProgressDialog pd(...);
connect(&watcher, SIGNAL(progressValueChanged(int)), &pd, SLOT(setValue(int)));
QFuture f = ...
watcher.setFuture(f);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文