自动关闭QMessageBox
我正在构建一个 Qt Symbian 项目,我想向用户显示一条通知,该通知应在几秒钟后自动关闭。我看到诺基亚在他们的用户界面中经常使用这个。
现在我正在使用下面的代码,以便用户可以关闭 QMessageBox,但如果可以在 1 或 2 秒后自动关闭 QMessageBox,我希望它。我如何使用 Qt 来做到这一点?
QMessageBox msgBox;
msgBox.setText("Hello!");
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
I'm building a Qt Symbian Project and I want to show a notification for the user that should auto close after some seconds. I have seen that Nokia uses this a lot in their ui.
Right now I'm using the code below so that the user can close the QMessageBox but I would like it if it was possible to auto close the QMessageBox after 1 or 2 seconds. How can I do this using Qt?
QMessageBox msgBox;
msgBox.setText("Hello!");
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
真的非常感谢!我的解决方案:
我创建了自己的类(MessageBox),这是我显示它的代码:
这是我的类:
Thanks really much! My solution:
I created my own class (MessageBox) this is my code for showing it:
This is my class:
我建议对
QMessageBox
进行子类化以添加您自己所需的行为...添加诸如
setAutoClose(bool)
和setAutoCloseTimeout(int)< 之类的方法会很有趣/code> 并在启用 AutoClose 选项时在
showEvent
上触发QTimer
!这样,您甚至可以更改
QMessageBox
的外观,并显示一条文字“此框将在 XXX 秒后自动关闭...”或进度条等...I would suggest to subclass
QMessageBox
to add your own desired behavior...It would be interesting to add methods like
setAutoClose(bool)
andsetAutoCloseTimeout(int)
and trigger aQTimer
onshowEvent
when the AutoClose option is enabled !This way, you could even alter the apparence of your
QMessageBox
and had a text saying "This box will close automatically in XXX seconds..." or a progress bar, etc...对于 Python,类似
QTimer.singleShot(5000, lambda : qm.done(0))
的方法可能有效。下面的示例将在 5 秒后超时关闭,如果超时则触发“否”选项。
For Python, something like
QTimer.singleShot(5000, lambda : qm.done(0))
may work.The below example will time out close after 5 seconds, triggering the "No" option if it times out.
相反,您可以使用
Singleshot
Timer 轻松关闭任何对话框或QLabel
:Instead you can use
Singleshot
Timer to close any dialog box orQLabel
with much ease:这可能会对某人有所帮助,
消息框会在 5 秒后关闭。
This may help someone,
The messageBox closes after 5 seconds.
使用此代码:
您得到:
因为 msgBOx (接收者)必须是 QtCore 对象..并且 QMessageBox 是 QtGui 的子类。请参阅 https:// /srinikom.github.io/pyside-docs/PySide/QtCore/QTimer.html#PySide.QtCore.PySide.QtCore.QTimer.singleShot
With this code:
you get:
Becouse msgBOx (the receiver) must be a QtCore object.. and QMessageBox subclassing QtGui. See https://srinikom.github.io/pyside-docs/PySide/QtCore/QTimer.html#PySide.QtCore.PySide.QtCore.QTimer.singleShot