QT5.1标准对话框按钮显示英文问题解决办法

**

QT5.1标准对话框按钮显示英文

**
QT标准对话框默认全是英文,需要在Qt安装目录下的translations,找到qt_zh_CN.qm和qt_zh_CN.ts。
直接用qt_zh_CN.qm,将其拷贝到工程目录,代码里根据路径调用:

		QDir dir(strPath + strUiLang);
		dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks | 	QDir::NoDotAndDotDot);
		dir.setNameFilters(QStringList()<<"*.qm");
		QFileInfoList list = dir.entryInfoList();
		//先清理之前的================
		for (int ix = 0; ix != vecCurLang.size(); ++ix)
		{
			qApp->removeTranslator(vecCurLang[ix].data());
		}
		m_pImpl->vecCurLang.clear();
		//==========================
		for (int i = 0; i < list.size(); ++i) 
		{
			QSharedPointer<QTranslator> ptTranslator(new QTranslator);
			if (ptTranslator->load(list[i].absoluteFilePath()))
			{
				vecCurLang.append(ptTranslator);
				qApp->installTranslator(ptTranslator.data());
			}
		}

但是,笔者,已经完成以上安装翻译文件,对于标准对话框的标准按钮,仍然显示英文,如QMessageBox里的显示yes,no;为何呢,对于qt4.8同样操作显示是中文,对于qt5.10为啥不翻译呢!
强烈的好奇心促使笔者一查到底:
Qt4.8源码:
标准对话框的按钮均来源于
QDialogButtonBox *buttonBox;
文字显示使用的是:
buttonText = standardButtonText(sbutton);

const char *QDialogButtonBoxPrivate::standardButtonText(QDialogButtonBox::StandardButton sbutton) const
{
    const char *buttonText = 0;
    bool gnomeLayout = (layoutPolicy == QDialogButtonBox::GnomeLayout);
    switch (sbutton) {
    case QDialogButtonBox::Ok:
        buttonText = gnomeLayout ? QT_TRANSLATE_NOOP("QDialogButtonBox", "&OK") : QT_TRANSLATE_NOOP("QDialogButtonBox", "OK");
        break;
    case QDialogButtonBox::Save:
        buttonText = gnomeLayout ? QT_TRANSLATE_NOOP("QDialogButtonBox", "&Save") : QT_TRANSLATE_NOOP("QDialogButtonBox", "Save");
        break;
    case QDialogButtonBox::Open:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Open");
        break;
    case QDialogButtonBox::Cancel:
        buttonText = gnomeLayout ? QT_TRANSLATE_NOOP("QDialogButtonBox", "&Cancel") : QT_TRANSLATE_NOOP("QDialogButtonBox", "Cancel");
        break;
    case QDialogButtonBox::Close:
        buttonText = gnomeLayout ? QT_TRANSLATE_NOOP("QDialogButtonBox", "&Close") : QT_TRANSLATE_NOOP("QDialogButtonBox", "Close");
        break;
    case QDialogButtonBox::Apply:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Apply");
        break;
    case QDialogButtonBox::Reset:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Reset");
        break;
    case QDialogButtonBox::Help:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Help");
        break;
    case QDialogButtonBox::Discard:
        if (layoutPolicy == QDialogButtonBox::MacLayout)
            buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Don't Save");
        else if (layoutPolicy == QDialogButtonBox::GnomeLayout)
            buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Close without Saving");
        else
            buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Discard");
        break;
    case QDialogButtonBox::Yes:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "&Yes");
        break;
    case QDialogButtonBox::YesToAll:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Yes to &All");
        break;
    case QDialogButtonBox::No:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "&No");
        break;
    case QDialogButtonBox::NoToAll:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "N&o to All");
        break;
    case QDialogButtonBox::SaveAll:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Save All");
        break;
    case QDialogButtonBox::Abort:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Abort");
        break;
    case QDialogButtonBox::Retry:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Retry");
        break;
    case QDialogButtonBox::Ignore:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Ignore");
        break;
    case QDialogButtonBox::RestoreDefaults:
        buttonText = QT_TRANSLATE_NOOP("QDialogButtonBox", "Restore Defaults");
        break;
    case QDialogButtonBox::NoButton:
        ;
    } // switch
    return buttonText;
}

打开qt_zh_CN.ts文件,里面有QDialogButtonBox的翻译部分,当然显示中文;
在这里插入图片描述

Qt5.10
同样的标准对话框按钮仍然使用QDialogButtonBox,但是源码部分,对于按钮的text显示进行了修改:

QPushButton *button = new QPushButton(QGuiApplicationPrivate::platformTheme()->standardButtonText(sbutton), q);

```cpp

QString QPlatformTheme::standardButtonText(int button) const
{
    return QPlatformTheme::defaultStandardButtonText(button);
}
//=============

QString QPlatformTheme::defaultStandardButtonText(int button)
{
    switch (button) {
    case QPlatformDialogHelper::Ok:
        return QCoreApplication::translate("QPlatformTheme", "OK");
    case QPlatformDialogHelper::Save:
        return QCoreApplication::translate("QPlatformTheme", "Save");
    case QPlatformDialogHelper::SaveAll:
        return QCoreApplication::translate("QPlatformTheme", "Save All");
    case QPlatformDialogHelper::Open:
        return QCoreApplication::translate("QPlatformTheme", "Open");
    case QPlatformDialogHelper::Yes:
        return QCoreApplication::translate("QPlatformTheme", "&Yes");
    case QPlatformDialogHelper::YesToAll:
        return QCoreApplication::translate("QPlatformTheme", "Yes to &All");
    case QPlatformDialogHelper::No:
        return QCoreApplication::translate("QPlatformTheme", "&No");
    case QPlatformDialogHelper::NoToAll:
        return QCoreApplication::translate("QPlatformTheme", "N&o to All");
    case QPlatformDialogHelper::Abort:
        return QCoreApplication::translate("QPlatformTheme", "Abort");
    case QPlatformDialogHelper::Retry:
        return QCoreApplication::translate("QPlatformTheme", "Retry");
    case QPlatformDialogHelper::Ignore:
        return QCoreApplication::translate("QPlatformTheme", "Ignore");
    case QPlatformDialogHelper::Close:
        return QCoreApplication::translate("QPlatformTheme", "Close");
    case QPlatformDialogHelper::Cancel:
        return QCoreApplication::translate("QPlatformTheme", "Cancel");
    case QPlatformDialogHelper::Discard:
        return QCoreApplication::translate("QPlatformTheme", "Discard");
    case QPlatformDialogHelper::Help:
        return QCoreApplication::translate("QPlatformTheme", "Help");
    case QPlatformDialogHelper::Apply:
        return QCoreApplication::translate("QPlatformTheme", "Apply");
    case QPlatformDialogHelper::Reset:
        return QCoreApplication::translate("QPlatformTheme", "Reset");
    case QPlatformDialogHelper::RestoreDefaults:
        return QCoreApplication::translate("QPlatformTheme", "Restore Defaults");
    default:
        break;
    }
    return QString();
}

然而问题来了,5.10的qt_zh_CN.ts文件中并没有针对QPlatformTheme的翻译部分,所有导致5.10标准的对话框按钮仍显示为英文;
解决办法:
不修改qt源码,直接在自己的类中添加对QPlatformTheme的翻译,然后在自己的翻译文件qm中,翻译一遍标准按钮部分即可;

void FrameWindow::Impl::RetranslateUi(FrameWindow* parent)
{
	//标准对话框按钮翻译:qt_zh_CN.qm文件中没有
	QCoreApplication::translate("QPlatformTheme", "OK");

	QCoreApplication::translate("QPlatformTheme", "Save");

	QCoreApplication::translate("QPlatformTheme", "Save All");

	QCoreApplication::translate("QPlatformTheme", "Open");

	QCoreApplication::translate("QPlatformTheme", "&Yes");

	QCoreApplication::translate("QPlatformTheme", "Yes to &All");

	QCoreApplication::translate("QPlatformTheme", "&No");

	QCoreApplication::translate("QPlatformTheme", "N&o to All");

	QCoreApplication::translate("QPlatformTheme", "Abort");

	QCoreApplication::translate("QPlatformTheme", "Retry");

	QCoreApplication::translate("QPlatformTheme", "Ignore");

	QCoreApplication::translate("QPlatformTheme", "Close");

	QCoreApplication::translate("QPlatformTheme", "Cancel");

	QCoreApplication::translate("QPlatformTheme", "Discard");

	QCoreApplication::translate("QPlatformTheme", "Help");

	QCoreApplication::translate("QPlatformTheme", "Apply");

	QCoreApplication::translate("QPlatformTheme", "Reset");

	QCoreApplication::translate("QPlatformTheme", "Restore Defaults");
}

更新ts文件,发布qm即可,英文变中文;
》》记录解决问题过程,方便自己,惠及他人!不足之处,欢迎指正!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值