c++ - 小部件仅在 for 循环结束时更新。我需要在 for 循环中逐步更新它
问题描述
如何使用下面的“for循环”来立即更新窗口。sleep(5) 只是查看物品是否立即被填满
我在一个简单的“类 MainWindow:public QMainWindow”中使用 Qt,带有一个按钮、一个 plainTextEdit 和一个 textEdit
for (int i=0; i < 10 ; i++ )
{
sentFrame = "toto"+i;
ui->alarmSent->addItem(sentFrame.toHex()); // filled at the end of for loop
ui->sentTest->insertPlainText(sentFrame.toHex()); // filled at the end of for loop
sleep(5);
}
解决方案
您不应该在 GUI 中使用 sleep 方法,因为它会长时间阻塞 GUI 所在的事件循环,解决方法是使用QTimer::singleShot()
+ QEventLoop
:
for (int i=0; i < 10 ; i++ )
{
sentFrame = "toto"+i;
ui->alarmSent->addItem(sentFrame.toHex());
ui->sentTest->insertPlainText(sentFrame.toHex());
QEventLoop loop;
QTimer::singleShot(5*1000, &loop, &QEventLoop::quit);
loop.exec();
}
推荐阅读
- java - 我如何循环嵌套对象并使用 lambda 和流验证一些计算
- java - XSD 两个元素之间的选择
- android-studio - 无法访问类“viewModel”。检查您的模块类路径是否存在缺失或冲突的依赖项
- c# - 在 Backgroundworker 中启动线程并保持线程可用
- flutter - 颤振 - 未处理的异常:SocketException:
- java - 如果在 Intellij 中手动添加 jar,则会发生编译错误
- python - 如何解决 CapsNet 的错误“考虑将元素转换为支持的类型”?
- sql - 如何将表的 clob 数据类型列与其他列一起导入 Hadoop
- python - 使用 Amazon Textract 时不受支持的文档格式,
- c++ - 在 QT Gui 中使用线程。它被摧毁