自动关闭QMessageBox

发布于 2024-08-21 03:01:37 字数 346 浏览 4 评论 0原文

我正在构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只等公子 2024-08-28 03:01:37

真的非常感谢!我的解决方案:

我创建了自己的类(MessageBox),这是我显示它的代码:

MessageBox msgBox;
msgBox.setText("Hello!");
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setAutoClose(true);
msgBox.setTimeout(3); //Closes after three seconds
msgBox.exec();

这是我的类:

class MessageBox : public QMessageBox

int timeout;
bool autoClose;
int currentTime;

void MessageBox::showEvent ( QShowEvent * event ) {
    currentTime = 0;
    if (autoClose) {
    this->startTimer(1000);
    }
}

void MessageBox::timerEvent(QTimerEvent *event)
{
    currentTime++;
    if (currentTime>=timeout) {
    this->done(0);
    }
}

Thanks really much! My solution:

I created my own class (MessageBox) this is my code for showing it:

MessageBox msgBox;
msgBox.setText("Hello!");
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setAutoClose(true);
msgBox.setTimeout(3); //Closes after three seconds
msgBox.exec();

This is my class:

class MessageBox : public QMessageBox

int timeout;
bool autoClose;
int currentTime;

void MessageBox::showEvent ( QShowEvent * event ) {
    currentTime = 0;
    if (autoClose) {
    this->startTimer(1000);
    }
}

void MessageBox::timerEvent(QTimerEvent *event)
{
    currentTime++;
    if (currentTime>=timeout) {
    this->done(0);
    }
}
懷念過去 2024-08-28 03:01:37

我建议对 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) and setAutoCloseTimeout(int) and trigger a QTimer on showEvent 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...

老旧海报 2024-08-28 03:01:37

对于 Python,类似 QTimer.singleShot(5000, lambda : qm.done(0)) 的方法可能有效。

下面的示例将在 5 秒后超时关闭,如果超时则触发“否”选项。

import sys

from PySide2.QtCore import *
from PySide2.QtWidgets import *

app = QApplication(sys.argv)

qm = QMessageBox()
qm.setText("Continue?")
qm.setStandardButtons(QMessageBox.Yes)
qm.addButton(QMessageBox.No)
qm.setDefaultButton(QMessageBox.No)
QTimer.singleShot(5000, lambda : qm.done(0))
if qm.exec_() == QMessageBox.Yes:
    print("Yes!")
else:
    print("No!")

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.

import sys

from PySide2.QtCore import *
from PySide2.QtWidgets import *

app = QApplication(sys.argv)

qm = QMessageBox()
qm.setText("Continue?")
qm.setStandardButtons(QMessageBox.Yes)
qm.addButton(QMessageBox.No)
qm.setDefaultButton(QMessageBox.No)
QTimer.singleShot(5000, lambda : qm.done(0))
if qm.exec_() == QMessageBox.Yes:
    print("Yes!")
else:
    print("No!")
·深蓝 2024-08-28 03:01:37

相反,您可以使用 Singleshot Timer 轻松关闭任何对话框或 QLabel

QTimer *timer;
QTimer::singleShot(10000, msgBox, SLOT(close()));

Instead you can use Singleshot Timer to close any dialog box or QLabel with much ease:

QTimer *timer;
QTimer::singleShot(10000, msgBox, SLOT(close()));
允世 2024-08-28 03:01:37

这可能会对某人有所帮助,

msgBox.button(QMessageBox::Ok)->animateClick(5000);

消息框会在 5 秒后关闭。

This may help someone,

msgBox.button(QMessageBox::Ok)->animateClick(5000);

The messageBox closes after 5 seconds.

合久必婚 2024-08-28 03:01:37

使用此代码:

QTimer *timer;
QTimer::singleShot(10000, msgBox, SLOT(close()));

您得到:

QObject::connect: Incompatible sender/receiver arguments
        QTimer::timeout() --> QMessageBox::

因为 msgBOx (接收者)必须是 QtCore 对象..并且 QMessageBox 是 QtGui 的子类。请参阅 https:// /srinikom.github.io/pyside-docs/PySide/QtCore/QTimer.html#PySide.QtCore.PySide.QtCore.QTimer.singleShot

With this code:

QTimer *timer;
QTimer::singleShot(10000, msgBox, SLOT(close()));

you get:

QObject::connect: Incompatible sender/receiver arguments
        QTimer::timeout() --> QMessageBox::

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文