qt-弹框提示-界面提醒

注意:前面都是基础讲解,如果有什么不懂的可以看看,但是如果只是追求实际运用场景,建议只看实际案例

原文

QMessageBox Class
The QMessageBox class provides a modal dialog for informing the user or for asking the user a question and receiving an answer.
它说,QMessageBox类提供了一个模态对话框,用于向用户显示信息、询问问题并接收用户回答。

setWindowFlags -> windowFlags
Window flags are a combination of a type (e.g. Qt::Dialog) and zero or more hints to the window system (e.g. Qt::FramelessWindowHint).If the widget had type Qt::Widget or Qt::SubWindow and becomes a window (Qt::Window, Qt::Dialog, etc.), it is put at position (0, 0) on the desktop. If the widget is a window and becomes a Qt::Widget or Qt::SubWindow, it is put at position (0, 0) relative to its parent widget.
Note: This function calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again…

它说:窗口标志是窗口类型(例如Qt::Dialog)与零个或多个窗口系统提示(例如Qt::FramelessWindowHint)的组合。若部件原类型为Qt::Widget或Qt::SubWindow后转为窗口类型(Qt::Window、Qt::Dialog等),该部件将被置于桌面坐标(0, 0)位置。若窗口类型部件转为Qt::Widget或Qt::SubWindow类型,则会相对于其父部件定位在(0, 0)坐标处。
注意:修改窗口标志时此函数会调用setParent(),这将导致部件被隐藏。必须调用show()才能使部件重新可见。

注意:setWindowFlags属于QDialog

setIcon(QMessageBox::Icon) -> icon
This property holds the message box’s icon
此属性保存消息框的图标

void QMessageBox::setWindowTitle(const QString &title)
This function shadows QWidget::setWindowTitle().
Sets the title of the message box to title. On macOS, the window title is ignored (as required by the macOS Guidelines).
This function was introduced in Qt 4.2.
它说,此函数遮蔽了QWidget::setWindowTitle()的功能。它将消息框的标题设置为指定的标题。在macOS系统上,窗口标题会被忽略(遵循macOS界面指南的要求)。此功能自Qt 4.2版本起引入。

void setText(const QString &text) -> QString text() const
This property holds the message box text to be displayed.
此属性保存要显示的消息框文本。

int QMessageBox::exec()
Shows the message box as a modal dialog, blocking until the user closes it.When using a QMessageBox with standard buttons, this function returns a StandardButton value indicating the standard button that was clicked. When using QMessageBox with custom buttons, this function returns an opaque value; use clickedButton() to determine which button was clicked.
Users cannot interact with any other window in the same application until they close the dialog, either by clicking a button or by using a mechanism provided by the window system.
将消息框显示为模态对话框,直到用户关闭它为止。当使用带有标准按钮的QMessageBox时,此函数返回一个StandardButton值,指示被点击的标准按钮。当使用带有自定义按钮的QMessageBox时,此函数返回一个不透明的值;使用clickedButton()来确定哪个按钮被点击。
用户必须通过点击按钮或窗口系统提供的机制关闭对话框,才能与同一应用程序中的其他窗口进行交互。

实际案例

void ClientWindow::updateMessage(const QString &message)
{
    bool isException = isExceptionLine(message);
    if(isException)
    {
        QMessageBox::warning(this, "异常",message);
    }
    else
    {
        QMessageBox msgBox(this);
        msgBox.setWindowFlags(msgBox.windowFlags() | Qt::WindowStaysOnTopHint);
        msgBox.setIcon(QMessageBox::Information);
        msgBox.setWindowTitle("执行结果");
        msgBox.setText(message);
        msgBox.exec();
    }
}

这个函数,可以在任意关键位置使用,主要也就用于调试过程,是一个非常有用的功能。当你的程序需要跑很长,短时间得不到结果时,用它作为提醒,相信很有帮助

效果展示

请添加图片描述

联动时间

这里,有个分辨是否为异常信息的小功能,如果有兴趣,请点击这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世转神风-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值