Qt 多个异步循环和 qApp->processEvents();
Qt Multiple Async While Loops and qApp->processEvents();
我正在尝试做一个项目,正在用QT GUI C 5.6.2在窗口上创建一些图形。我有两种名为" createvertilityspeedIndicator"answers" createAieRspeedIndicator"的方法。这些方法需要使用一段时间(1(循环创建一些图形并使用qapp-> processevents((;在窗户上,当其中一个正在工作时,他们做得很好,而另一个则是停用的。但是我需要同时且始终运行它们。
我该怎么做才能同时运行它们。
非常感谢
解决方案是反转控制流。while() { ... processEvents() ... }
是异步代码中的一种反图案,因为它假设您具有控制源,而您确实没有。您很幸运,您没有用完堆栈,因为processEvents
可能会重新输入createXxx
方法。
这是转换的完整示例:
// Input
void Class::createVerticalSpeedIndicator() {
for (int i = 0; i < 100; i ++) {
doStep(i);
QCoreApplication::processEvents();
}
}
// Step 1 - factor out state
void Class::createVerticalSpeedIndicator() {
int i = 0;
while (i < 100) {
doStep(i);
QCoreApplication::processEvents();
i++;
}
};
// Step 2 - convert to continuation form
void Class::createVerticalSpeedIndicator() {
int i = 0;
auto continuation = [=]() mutable {
if (!(i < 100))
return false;
doStep(i);
QCoreApplication::processEvents();
i++;
return true;
};
while (continuation());
};
// Step 3 - execute the continuation asynchronously
auto Class::createVerticalSpeedIndicator() {
int i = 0;
return async(this, [=]() mutable {
if (!(i < 100))
return false;
doStep(i);
i++; // note the removal of processEvents here
return true;
});
};
template <typename F> void async(QObject * parent, F && continuation) {
auto timer = new QTimer(parent);
timer->start(0);
connect(timer, &QTimer::timeout, [timer, c = std::move(continuation)]{
if (!c())
timer->deleteLater();
});
}
那时,您可以将相同的转换应用于createAirSpeedIndicator
并在类的构造函数中启动:
Class::Class(QWidget * parent) : QWidget(parent) {
...
createVerticalSpeedIndicator();
createAirSpeedIndicator();
}
这两个任务都会在主线程中不同步和伪续线运行,即每个任务都将执行一个步骤。
假设我们想链接任务,即仅在上一个任务完成后才开始任务。用户代码中的修改可以很简单:
Class::Class(QWidget * parent) : QWidget(parent) {
...
createVerticalSpeedIndicator()
>> createAirSpeedIndicator()
>> someOtherTask();
}
async
功能现在必须返回允许建立此类连接的类:
struct TaskTimer {
QTimer * timer;
TaskTimer & operator>>(const TaskTimer & next) {
next.timer->stop();
connect(timer, &QObject::destroyed, next.timer, [timer = next.timer]{
timer->start(0);
});
timer = next.timer;
return *this;
}
};
template <typename F> TaskTimer async(QObject * parent, F && continuation) {
TaskTimer task{new QTimer(parent)};
task.timer->start(0);
connect(task.timer, &QTimer::timeout,
[timer = task.timer, c = std::move(continuation)]{
if (!c())
timer->deleteLater();
});
return task;
}
相关文章:
- EASTL矢量<向量<int>>连续的
- 我应该在线程上调用 processEvents() 吗?
- QCoreApplication::processEvents-为什么必须仅从主线程调用它
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- Qt 多个异步循环和 qApp->processEvents();
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在