Qt QMessageBox使用详解

本文详细的介绍了QMessageBox控件的各种操作,例如:消息提示框的使用、判断消息提示框的按钮、标准图标和自定义图标、定时关闭、自定义样式等操作。

本文作者原创,转载请附上文章出处与本文链接。

Qt QMessageBox使用详解目录

1. 标准信息提示框

2. 判断提示框按钮

3. 提示框自带图标

4. 定时关闭提示框

5. QMessageBox自定义样式


1. 标准信息提示框

    information:标准信息提示框

    QMessageBox::warning(this,"Title","Error Message");
    QMessageBox::information(NULL,  "Title",  "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    //QMessageBox ::Yes, QMessageBox :: No, QMessageBox :: Retry , QMessageBox :: Ignore , QMessageBox :: Ok , QMessageBox :: Cancel    //可选

information原型:StandardButton QMessageBox::information(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton) [static]
  • 第一个参数是父控件指针
  • 第二个参数是标题
  • 第三个参数是内容
  • 第四个参数是窗口里面要多少个按钮(默认为OK)
  • 第五个参数指定按下Enter时使用的按钮。(默认为NoButton,此时QMessageBox会自动选择合适的默认值。)

2. 判断提示框按钮

    switch(QMessageBox::information(this,"Warning",tr("Save changes to document?"),
        QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save))
    {
    case QMessageBox::Save:
        qDebug() << " Warning button / Save " << endl;;
        break;
    case QMessageBox::Discard:
        qDebug() << " Warning button / Discard " << endl;;
        break;
    case QMessageBox::Cancel:
        qDebug() << " Warning button / Cancel " << endl;;
        break;
    default:
        break;
    }

3. 提示框自带图标

    //ico弹窗
    QMessageBox::critical(this,"Critical",tr("tell user a critical error"));              //错误
    //QMessageBox::warning 警告 QMessageBox::critical 错误 QMessageBox::Information 叹号 QMessageBox::Question 问号
    //自定义ico
    QMessageBox customMsgBox;
    customMsgBox.setWindowTitle("Custom message box");
    QPushButton *lockButton = customMsgBox.addButton(tr("Lock"),QMessageBox::ActionRole);
    QPushButton *unlockButton = customMsgBox.addButton(tr("Unlock"),QMessageBox::ActionRole);
    QPushButton *cancelButton = customMsgBox.addButton(QMessageBox::Cancel);
    customMsgBox.setIconPixmap(QPixmap("D:/ico.ico"));//可以设置png图形
    customMsgBox.setText(tr("This is a custom message box"));
    customMsgBox.exec();

    if(customMsgBox.clickedButton() == lockButton)
        qDebug() << " Custom MessageBox / Lock ";
    if(customMsgBox.clickedButton() == unlockButton)
        qDebug() << " Custom MessageBox / Unlock ";
    if(customMsgBox.clickedButton() == cancelButton)
        qDebug() << " Custom MessageBox / Cancel ";

4. 定时关闭提示框

    QMessageBox *box = new QMessageBox(QMessageBox::Information,"Title","Content");
    QTimer::singleShot(1500,box,SLOT(accept())); //也可将accept改为close
    box->exec();//box->show();都可以

5. QMessageBox自定义样式

    自定义消息提示框一般有两种常用的方法:

    方法一 :出自 一去、二三里 Qt 之自定义界面(QMessageBox)_一去丶二三里的博客-CSDN博客_qt自定义qmessagebox

    方法二:自定义MessageBox样式_深夜程序员的博客-CSDN博客_自定义messagebox

    QT官方API:https://doc.qt.io/qt-5/qmessagebox.html

 其它文章 :

QT TextEdit控件_双子座断点的博客-CSDN博客_qt textedit

QT QComboBox使用详解_双子座断点的博客-CSDN博客

QT QtableView操作详解_双子座断点的博客-CSDN博客_qtableview增删改查

Qt QStandardItemModel(1.超级详细用法)_双子座断点的博客-CSDN博客_qstandardmodel

Qt QStandardItemModel(2.超级详细函数)_双子座断点的博客-CSDN博客_qstandarditemmodel点击事件

QT QRadioButton使用详解_双子座断点的博客-CSDN博客_qt radiobutton

QT QLineEdit使用详解_双子座断点的博客-CSDN博客_qt qlineedit

Qt QMessageBox使用详解_双子座断点的博客-CSDN博客_qt message

QChart折线图、饼状图、条形图、曲线图_双子座断点的博客-CSDN博客_qchart样式

QChart属性详解_双子座断点的博客-CSDN博客_setanimationoptions

QCharts QValueAxis使用_双子座断点的博客-CSDN博客_qvalueaxis

Qt 5 等待提示框(开源 动态图)_双子座断点的博客-CSDN博客_qt 等待对话框

QtDataVisualization 数据3D可视化_双子座断点的博客-CSDN博客_qtdatavisualization

  • 22
    点赞
  • 119
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

双子座断点

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

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

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

打赏作者

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

抵扣说明:

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

余额充值