问题对话框
对应API
[static] QMessageBox::StandardButton QMessageBox::question(
QWidget *parent,
const QString &title,
const QString &text,
QMessageBox::StandardButtons buttons = StandardButtons(Yes | No),
QMessageBox::StandardButton defaultButton = NoButton)
举例:
//参数1:父窗口
//参数2:对话框标题
//参数3:对话框提示文本
//参数4:对话框按钮
//参数5:对话框刚弹出时默认选择的按钮
//返回值:点击的按钮
auto choice = QMessageBox::question(this,
"Question",
"你是否想退出,请选择:",
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Ok);
switch (choice) {
case QMessageBox::Ok:
//do something
qDebug() << "选择了ok";
break;
case QMessageBox::Cancel:
//do something
qDebug() << "选择了cancel";
break;
default:
break;
}
提示对话框
对应API
[static] QMessageBox::StandardButton QMessageBox::information(
QWidget * parent,
const QString & title,
const QString & text,
QMessageBox::StandardButtons buttons = Ok,
QMessageBox::StandardButton defaultButton = NoButton)
举例:
//参数1:父窗口
//参数2:对话框标题
//参数3:对话框提示文本
//其余几个参数使用默认的就好了,提示对话框不需要什么按钮
//返回值:点击的按钮
auto choice = QMessageBox::information(this, "Information", "即将退出!");
switch (choice) {
case QMessageBox::Ok:
//do something
qDebug() << "选择了ok";
break;
case QMessageBox::Cancel:
//do something
qDebug() << "选择了cancel";
break;
default:
break;
}
警告对话框
对应API
[static] QMessageBox::StandardButton QMessageBox::warning(
QWidget * parent,
const QString & title,
const QString & text,
QMessageBox::StandardButtons buttons = Ok,
QMessageBox::StandardButton defaultButton = NoButton)
举例:
//参数1:父窗口
//参数2:对话框标题
//参数3:对话框提示文本
//参数4:对话框按钮
//参数5:对话框刚弹出时默认选择的按钮
//返回值:点击的按钮
auto choice = QMessageBox::warning(this, "Warning", "会被删除!",
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Cancel);
switch (choice) {
case QMessageBox::Ok:
//do something
qDebug() << "选择了ok";
break;
case QMessageBox::Cancel:
//do something
qDebug() << "选择了cancel";
break;
default:
break;
}
错误对话框
对应API
[static] QMessageBox::StandardButton QMessageBox::critical(
QWidget * parent,
const QString & title,
const QString & text,
QMessageBox::StandardButtons buttons = Ok,
QMessageBox::StandardButton defaultButton = NoButton)
举例:
//参数1:父窗口
//参数2:对话框标题
//参数3:对话框提示文本
//其余几个参数使用默认的就好了,错误对话框不需要什么按钮
//返回值:点击的按钮
auto choice = QMessageBox::critical(this, "critical", "错误操作!");
switch (choice) {
case QMessageBox::Ok:
//do something
qDebug() << "选择了ok";
break;
case QMessageBox::Cancel:
//do something
qDebug() << "选择了cancel";
break;
default:
break;
}
关于对话框
就是一个简单的对话框,可以介绍一些版本信息什么的
对应API
[static] void QMessageBox::about(
QWidget * parent,
const QString & title,
const QString & text)
举例:
//参数1:父窗口
//参数2:对话框标题
//参数3:对话框提示文本
QMessageBox::about(this, "about", "版本:v1.1");
自定义消息对话框
相关API
- 设置标题
-
void QMessageBox::setWindowTitle(const QString &title)
- 设置自定义图片
-
void setIconPixmap(const QPixmap &pixmap)
- 设置文本
-
void setText(const QString &text
- 设置按钮
-
//自定义按钮role一般设置为QMessageBox::ButtonRole::ActionRole //返回值:添加的按钮 QPushButton *QMessageBox::addButton(const QString &text, QMessageBox::ButtonRole role)
- 获取点击的按钮
-
QAbstractButton *QMessageBox::clickedButton() const
举例:
QMessageBox custom_message_box;
//1.设置标题
custom_message_box.setWindowTitle("自定义对话框");
//2.设置图片
custom_message_box.setIconPixmap(QPixmap(":/11.png").scaled(20, 20));
//3.设置文本
custom_message_box.setText("这是一个自定义对话框");
//4.添加按钮
QPushButton * btn_yes = custom_message_box.addButton("YES", QMessageBox::ButtonRole::ActionRole);
QPushButton * btn_no = custom_message_box.addButton("NO", QMessageBox::ButtonRole::ActionRole);
//模态显示对话框
custom_message_box.exec();
//5.获取点击的按钮
if (custom_message_box.clickedButton() == btn_yes) {
//do something...
} else if (custom_message_box.clickedButton() == btn_no) {
//do something...
}
学习链接:https://github.com/0voice