QMessageBox 设置中文按钮

QMessageBox 设置中文按钮(返回值判断)

(一)解决乱码

#pragma execution_character_set(“UTF-8”)

(二)按钮修改为中文

方法一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 创建一个question弹出对话框,添加两个按钮:Yes和No
QMessageBox *msgBox = new QMessageBox(QMessageBox::Question, tr("对话框标题"), tr("对话框内容"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);

// 将原本显示“Yes”的按钮改为显示“是”
msgBox->button(QMessageBox::Yes)->setText("是");
// 将原本显示“No”的按钮改为显示“否”
msgBox->button(QMessageBox::No)->setText("否");
// 将原本显示“xx”的按钮改为显示“不要”
msgBox->button(QMessageBox::Cancel)->setText("不要");

// 弹出对话框
int ret = msgBox->exec();


switch (ret) {
case QMessageBox::Yes:
qDebug() << "1";
break;
case QMessageBox::No:
qDebug() << "2";
break;
case QMessageBox::Cancel:
qDebug() << "3";
break;
}

方法二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
QMessageBox box(QMessageBox::Question,"标题","弹出框按钮更改为中文!");
box.setStandardButtons (QMessageBox::Ok|QMessageBox::Cancel);
box.setButtonText (QMessageBox::Ok,QString("确 定"));
box.setButtonText (QMessageBox::Cancel,QString("取 消"));
int ret1 = box.exec ();

switch (ret1) {
case QMessageBox::Ok:
qDebug() << "1";
break;
case QMessageBox::Cancel:
qDebug() << "2";
break;
}

方法三

1
2
3
4
QMessageBox mess(QMessageBox::Question, "删除提示", "确认删除所选组件?", NULL);  
QPushButton *okButton = mess.addButton(tr("确定"), QMessageBox::AcceptRole);
QPushButton *cancelButton = mess.addButton(tr("取消"),QMessageBox::RejectRole);
mess.exec();

QMessageBox设置 参考链接: https://www.devbean.net/2012/09/qt-study-road-2-standard-dialogs-qmessagebox/