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()来确定哪个按钮被点击。

用户必须通过点击按钮或窗口系统提供的机制关闭对话框,才能与同一应用程序中的其他窗口进行交互。

实际案例

c 复制代码
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();
    }
}

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

联动时间

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

相关推荐
moonsims19 分钟前
星闪在跨域无人化系统作用
开发语言·php
Iruoyaoxh1 小时前
栈和队列~
java·开发语言
Hrain-AI1 小时前
2026 企业 AI 编程智能体实战:Codex 与 Claude Code
开发语言·人工智能·kotlin
wdfk_prog2 小时前
嵌入式面试真题第 13 题:单硬件 Timer 下的高精度多路软件定时器架构设计
c语言·开发语言·面试·职场和发展·嵌入式
oort1232 小时前
吃上了自家的细糠,还挺丝滑,用起来手感还行,OortCloud发布新版AI编程平台,下载 OortCodex,Token多,免费薅
大数据·开发语言·人工智能·ai编程
slandarer2 小时前
MATLAB | 泰勒图绘制,支持各种角度范围
开发语言·数学建模·matlab·nature·顶刊·泰勒图
mct1232 小时前
c++ iconv 字符utf-8转换gb2312失败
开发语言·c++
Marst Code4 小时前
Python 3.9 已停止维护!从 3.9 到 3.14 全版本深度对比,生产环境该选哪个?
开发语言·python
ZJH__GO4 小时前
网络编程v2--多客户端互通
java·运维·服务器·开发语言·计算机网络
万岳科技系统开发5 小时前
智慧医院小程序开发推动医疗服务流程全面线上化
大数据·开发语言·人工智能