Qt开发之路9---消息对话框QMessageBox

QMessageBox用于显示消息提示。我们一般会使用其提供的几个 static 函数:

  • 1.显示关于对话框QMessageBox::about
void about(QWidget * parent, const QString & title, const QString & text);

这是一个最简单的对话框,其标题是 title,内容是 text,父窗口是 parent。对话框只有一个 OK 按钮。

  • 2.显示关于 Qt 对话框QMessageBox::aboutQt。该对话框用于显示有关 Qt 的信息。
void aboutQt(QWidget * parent, const QString & title = QString());
  • 3.显示严重错误对话框QMessageBox::critical
StandardButton critical(QWidget * parent, 
						const QString & title, 
						const QString & text, 
						StandardButtons buttons = Ok, 
						StandardButton defaultButton = NoButton);

这个对话框将显示一个红色的错误符号。我们可以通过 buttons 参数指明其显示的按钮。默认情况下只有一个 Ok 按钮,我们可以使用StandardButtons类型指定多种按钮。

  • 4.显示信息对话框QMessageBox::information。这个对话框提供一个普通信息图标。
StandardButton information(QWidget * parent, 
							const QString & title, 
							const QString & text, 
							StandardButtons buttons = Ok, 
							StandardButton defaultButton = NoButton)
  • 5.显示问题对话框QMessageBox::question。这个对话框提供一个问号图标,并且其显示的按钮是“是”和“否”。
StandardButton question(QWidget * parent,
						const QString & title, 
						const QString & text, 
						StandardButtons buttons = StandardButtons( Yes | No ), 
						StandardButton defaultButton = NoButton) 
  • 6.显示警告对话框QMessageBox::warning。这个对话框提供一个黄色叹号图标。
StandardButton warning(QWidget * parent, 
						const QString & title, 
						const QString & text, 
						StandardButtons buttons = Ok, 
						StandardButton defaultButton = NoButton)

在这里插入图片描述

例程:

    if (QMessageBox::Yes == QMessageBox::question(this,
                  tr("Question"), tr("Are you OK?"),
                  QMessageBox::Yes | QMessageBox::No,
                  QMessageBox::Yes))
    {
        QMessageBox::information(this, tr("Hit"),tr("I clicked Yes!"));
    }
    else
    {
        QMessageBox::information(this, tr("Hit"),tr("I'm not!"));
    }

QMessageBox::question()来询问一个问题

  • 第一个参数是这个对话框的父窗口是this
    QMessageBox是QDialog的子类,这意味着它的初始显示位置将会是在 parent 窗口的中央。
  • 第二个参数是对话框的标题
  • 第三个参数是我们想要显示的内容
    这里就是我们需要询问的文字。下面,我们使用或运算符(|)指定对话框应该出现的按钮Yes 和一个 No。
  • 最后一个参数指定默认选择的按钮
    这个函数有一个返回值,用于确定用户点击的是哪一个按钮。这是一个模态对话框,我们可以直接获取其返回值。
    在这里插入图片描述

QMessageBox类的 static 函数优点是方便使用,缺点也很明显:非常不灵活。我们只能使用简单的几种形式。为了能够定制QMessageBox细节,我们可以使用QMessageBox的属性设置 API,详情可查看说明文档。

上一篇:Qt开发之路8—对话框QDialog
下一篇:Qt开发之路10—文件对话框QFileDialog

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值