以下代码在 2 秒后关闭了我的 QMessageBox
。但是我的文本显示框关闭时,它在框关闭之前闪烁得非常快。这是怎么回事?
QMessageBox *msgBox = new QMessageBox();
msgBox->setText("Coördinate is being created, please wait...");
msgBox->show();
QTimer::singleShot(2000, msgBox, SLOT(hide()));
这显示了,然后就在关闭之前我可以看到文本。
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/