在计时器上关闭 QMessageBox,setText 不显示
Closing QMessageBox on timer, setText not showing
以下代码在 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
您的代码很好,至少您显示的部分是这样。
我自己测试了它,它没有任何问题。
但请记住,关闭和隐藏对话框是两件不同的事情。
您只需隐藏 Window。 Window 仍将存在于内存中。
也许您想在计时器中调用 "close slot" 并将 windows 属性设置为 "delete on close":
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()));
如果这不是您描述的结果的原因,您必须提供更多信息。
更新后,
当然,如果您不return立即从调用函数中退出,这是一个问题 - 您正在阻止事件循环,因此会更新所有小部件!
可能的解决方案
您可以创建 WriteMultipleACLCommands
Q_INVOKABLE(或一个插槽)并将其调用为 Qt::QueuedConnection
:
QMetaObject::invokeMethod(singleton_SerialPortManager, "WriteMultipleACLCommands", Qt::QueuedConnection);
这样您就可以 post 将一个事件添加到事件队列并立即 return 。之后消息框将收到更新,然后在某个时候 WriteMultipleACLCommands
也会被调用。
以下代码在 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
您的代码很好,至少您显示的部分是这样。 我自己测试了它,它没有任何问题。 但请记住,关闭和隐藏对话框是两件不同的事情。 您只需隐藏 Window。 Window 仍将存在于内存中。 也许您想在计时器中调用 "close slot" 并将 windows 属性设置为 "delete on close":
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()));
如果这不是您描述的结果的原因,您必须提供更多信息。
更新后,
当然,如果您不return立即从调用函数中退出,这是一个问题 - 您正在阻止事件循环,因此会更新所有小部件!
可能的解决方案
您可以创建 WriteMultipleACLCommands
Q_INVOKABLE(或一个插槽)并将其调用为 Qt::QueuedConnection
:
QMetaObject::invokeMethod(singleton_SerialPortManager, "WriteMultipleACLCommands", Qt::QueuedConnection);
这样您就可以 post 将一个事件添加到事件队列并立即 return 。之后消息框将收到更新,然后在某个时候 WriteMultipleACLCommands
也会被调用。