当前位置 博文首页 > 木木木 的博客:Qt开发之路9---消息对话框QMessageBox
QMessageBox用于显示消息提示。我们一般会使用其提供的几个 static 函数:
void about(QWidget * parent, const QString & title, const QString & text);
这是一个最简单的对话框,其标题是 title,内容是 text,父窗口是 parent。对话框只有一个 OK 按钮。
void aboutQt(QWidget * parent, const QString & title = QString());
StandardButton critical(QWidget * parent,
const QString & title,
const QString & text,
StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton);
这个对话框将显示一个红色的错误符号。我们可以通过 buttons 参数指明其显示的按钮。默认情况下只有一个 Ok 按钮,我们可以使用StandardButtons类型指定多种按钮。
StandardButton information(QWidget * parent,
const QString & title,
const QString & text,
StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton)
StandardButton question(QWidget * parent,
const QString & title,
const QString & text,
StandardButtons buttons = StandardButtons( Yes | No ),
StandardButton defaultButton = NoButton)
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()来询问一个问题
QMessageBox类的 static 函数优点是方便使用,缺点也很明显:非常不灵活。我们只能使用简单的几种形式。为了能够定制QMessageBox细节,我们可以使用QMessageBox的属性设置 API,详情可查看说明文档。
上一篇:Qt开发之路8—对话框QDialog
下一篇:Qt开发之路10—文件对话框QFileDialog