Skip to content

QDialog

1 弹窗按钮设置中文

Qt默认弹窗都是英文,需要自己手动修改下才能改为中文。

qt5_create_translation的话,直接修改.ts文件。

1.1 QInputDialog

修改前

 QString dlgTitle = "系统设置登录";
 QString txtLabel = "请输入管理员密码";
 QString defaultInput = "******";
 QLineEdit::EchoMode echoMode = QLineEdit::Password;
 bool ok = false;
 QString text = 
     QInputDialog::getText(this, dlgTitle, txtLabel, echoMode, defaultInput, &ok);
 if (ok && !text.isEmpty()) {
 }

修改后

QString dlgTitle = "系统设置登录";
QString txtLabel = "请输入管理员密码";
QString defaultInput = "******";
QLineEdit::EchoMode echoMode = QLineEdit::Password;
bool ok = false;
QInputDialog *input_dialog = new QInputDialog(this);
input_dialog->setOkButtonText("确定");
input_dialog->setCancelButtonText("取消");
input_dialog->setWindowTitle(dlgTitle);
input_dialog->setTextValue(defaultInput);
input_dialog->setTextEchoMode(echoMode);
input_dialog->setLabelText(txtLabel);
QString text;
if(QDialog::Accepted == input_dialog->exec()) {
ok = true;
text = input_dialog->textValue();
} else {
ok = false;
}
if (ok) {
}

1.2 QDialogButtonBox

修改前(ui_XXX.h)

buttonBox = new QDialogButtonBox(ReportCustomDialog);
buttonBox->setObjectName(QStringLiteral("buttonBox"));
buttonBox->setMinimumSize(QSize(150, 40));
buttonBox->setMaximumSize(QSize(150, 40));
buttonBox->setOrientation(Qt::Horizontal);
buttonBox->setStandardButtons(QDialogButtonBox::Discard|QDialogButtonBox::Ok);
buttonBox->setCenterButtons(false);

修改后

buttonBox = new QDialogButtonBox(ReportCustomDialog);
buttonBox->setObjectName(QStringLiteral("buttonBox"));
buttonBox->setMinimumSize(QSize(150, 40));
buttonBox->setMaximumSize(QSize(150, 40));
buttonBox->setOrientation(Qt::Horizontal);
buttonBox->setStandardButtons(QDialogButtonBox::NoButton);
buttonBox->setCenterButtons(false);
ui->buttonBox->addButton("确认", QDialogButtonBox::AcceptRole);
ui->buttonBox->addButton("取消", QDialogButtonBox::RejectRole);