c++ - 在函数内部的 while 循环中使用 processEvents()
问题描述
我有一个插槽功能
void MobilePlatform::slot_lineFollow(bool checked)
{
while(checked){
...
QCoreApplication::processEvents();
}
if(!checked){
....
}
}
此函数是从QCheckBox请求的函数,当按下复选框时,变量checked
为真并进入 while 循环,当未按下复选框checked
时,变量为false
并从 while 循环中退出并进入 if块,这是正常的,但我不明白的是,为什么在下一步中去QcoreApplication::processEvents()
,当它出去时,变量checked
是真的。
知道为什么会发生这些吗?
提前致谢!
解决方案
你已经被重新进入事件循环的后果所困扰。现在你知道为什么这不是一个好主意了。永远不要阻塞在主线程中,永远不要重新进入事件循环。反转控制流以准确反映异步现实:
class MobilePlatform : public QWidget {
Q_OBJECT
using self = MobilePlatform;
using base_class = QWidget;
using spin_handler = void (self::*)();
QBasicTimer m_spinTimer;
...
Q_SIGNAL void spinSignal();
void spin(spin_handler);
void despin(spin_handler);
void follower();
Q_SLOT void slot_lineFollow(bool checked);
protected:
void timerEvent(QTimerEvent *) override;
public:
...
};
void MobilePlatform::timerEvent(QTimerEvent *ev) {
if (ev->timerId() == m_spinTimer.timerId())
Q_EMIT spinSignal();
else
base_class::timerEvent(ev);
}
void MobilePlatform::spin(spin_handler handler) {
if (!m_spinTimer.isActive())
m_spinTimer.start(0, this);
connect(this, &self::spinSignal, this, handler, Qt::UniqueConnection | Qt::DirectConnection);
Q_ASSERT(m_spinTimer.isActive());
}
void MobilePlatform::despin(spin_handler handler) {
static const auto signal = QMetaMethod::fromSignal(&self::spinSignal);
Q_ASSERT(signal.isValid());
disconnect(this, &self::spinSignal, this, handler);
auto const hasHandlers = isSignalConnected(signal);
if (!hasHandlers)
m_spinTimer.stop();
Q_ASSERT(m_spinTimer.isActive() == hasHandlers);
}
void MobilePlatform::follower() {
/* code that runs when following */
...
}
void MobilePlatform::slot_lineFollow(bool checked) {
if (checked()) {
spin(&self::follower);
} else {
despin(&self::follower);
...
}
}
推荐阅读
- linux - 如何使用 gdb 在寄存器偏移处读取内存
- python - 有没有办法在 python 列表中切换元素?
- prawn - 在 Prawn 中构建内容块?
- python - 空返回数组
- python - messagex = int((SCREENWIDTH - GAME_SPRITES['message'].get_height())/2 ^ SyntaxError: invalid syntax)
- java - equals() 的参数类型不太可能:String 似乎与 Scanner 无关
- node.js - 无法在数字海洋应用平台上部署带有节点后端的 React 应用
- websocket - AWS NLB + NGINX Ingress + websockets == 502 Bad Gateway or Bad Handshake
- python - 网页抓取python bs4中的属性错误
- jquery - 想在另一个视图页面显示响应短信ajax成功功能