c++ - 在计时器上关闭 QMessageBox,setText 不显示

标签 c++ qt qmessagebox

以下代码在 2 秒后关闭了我的 QMessageBox。但是我的文本显示框关闭时,它在框关闭之前闪烁得非常快。这是怎么回事?

 QMessageBox *msgBox = new QMessageBox();
 msgBox->setText("Coördinate is being created, please wait...");
 msgBox->show();
 QTimer::singleShot(2000, msgBox, SLOT(hide()));

enter image description here

这显示了,然后就在关闭之前我可以看到文本。

update

在单线程程序中工作:方法 WriteMultipleACLCommands() 占用了大量时间。也许这就是问题所在?

  QMessageBox *msgBox = new QMessageBox();
  msgBox->setText("Coördinate is being created, please wait...");
  msgBox->show();
  QTimer::singleShot(2000, msgBox, SLOT(hide()));
  singleton_SerialPortManager->WriteMultipleACLCommands();
  //function writes a few bytes onto a serial connection

最佳答案

更新后,

当然,如果您不立即从调用函数返回,这是一个问题 - 您会阻塞事件循环,因此会更新所有小部件!

可能的解决方案

您可以制作WriteMultipleACLCommands Q_INVOKABLE(或槽)并将其作为Qt::QueuedConnection调用:

QMetaObject::invokeMethod(singleton_SerialPortManager, "WriteMultipleACLCommands", Qt::QueuedConnection);

通过这种方式,您只需将事件发布到事件队列并立即返回。之后消息框将收到更新,然后在某个时候 WriteMultipleACLCommands 也会被调用。

关于c++ - 在计时器上关闭 QMessageBox,setText 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33476507/

相关文章:

c++ - 从 HDRi LatLong 贴图直接渲染到浮点立方体贴图 - 而不是 HDR

c++ - 如何使用 QT WebEngine 发送 HTTPHeader?

c++ - 获取 QMap::iterator 的位置 (int)

c++ - 使用 Qt Creator 构建 Stanford CS106B 项目的问题

python - 如何在pyqt MessageBox中使用变量

Python - 生成给定长度的所有可能的 1 和 0 数组的算法

c++ - 在 Snow Leopard 下编译 STLport 的问题

c++ - 解密后出现“消息哈希或 MAC 无效”异常

python - QtGui.QMessageBox.information 和自定义

qt - 如何使用QMessageBox显示纯文本?