2015-11-02 263 views
0

下面的一段代码在2秒后关闭我的QMessageBox。但是我的文字显示了盒子关闭时的情况,它在盒子关闭之前非常快速地闪烁。这里发生了什么?关闭定时器上的QMessageBox,setText不显示

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

这说明,然后就关闭之前,我可以看到的文字。

更新

在单线程程序工作:方法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 
+0

我试过了你的代码,我没有看到闪烁的东西。我在Ubuntu 14.04上使用Qt 5.5.1。 – agold

+0

它看起来不像代码有任何问题。这更可能是其他地方的东西。 –

回答

1

更新后,

当然是一个问题,如果你不从调用函数返回的时候了 - 你是阻塞事件循环,因此更新到所有小工具!

可能的解决方案

您可以WriteMultipleACLCommands Q_INVOKABLE(或槽)并调用它Qt::QueuedConnection

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

这样,你只是事件发布到事件队列和返回马上。之后,消息框将收到更新,然后在某个时间点WriteMultipleACLCommands也会被调用。

0

你的代码没问题,至少是你展示的部分。 我自己测试过,它没有任何问题。 但请记住,关闭和隐藏对话框是两回事。 你只是隐藏窗口。该窗口仍将存在内存中。 也许你想打电话给你的计时器“关闭槽”,并设置窗口属性设置为“关闭时删除”:

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

如果这不是你所描述的效果的原因,你必须给一些更多的信息。