Qt自带翻译无效

Qt自带翻译无效

问题:在使用 QMessageBox 时,Qt 标准按钮没有被翻译

QMessageBox::information(nullptr, tr("tips"), tr("Are yue sure?"), QMessageBox::Yes);

如上代码,使用了标准按钮 QMessageBox::Yes。因此需要将 qt 翻译加载到程序中

解决方法

  1. 加载 Qt 翻译

    • QLibraryInfo
      • 通过 QLibraryInfo 可以获取 Qt 指定的翻译目录,QLibrary 支持目录包括如下:
    onstantValueDescription
    LibraryInfo::PrefixPath0The default prefix for all paths.
    ibraryInfo::DocumentationPath1The location for documentation upon install.
    ibraryInfo::HeadersPath2The location for all headers.
    ibraryInfo::LibrariesPath3The location of installed libraries.
    ibraryInfo::LibraryExecutablesPath4The location of installed executables required by libraries at runtime.
    ibraryInfo::BinariesPath5The location of installed Qt binaries (tools and applications).
    ibraryInfo::PluginsPath6The location of installed Qt plugins.
    ibraryInfo::ImportsPath7The location of installed QML extensions to import (QML 1.x).
    ibraryInfo::Qml2ImportsPath8The location of installed QML extensions to import (QML 2.x).
    ibraryInfo::ArchDataPath9The location of general architecture-dependent Qt data.
    ibraryInfo::DataPath10The location of general architecture-independent Qt data.
    ibraryInfo::TranslationsPath11The location of translation information for Qt strings.
    ibraryInfo::ExamplesPath12The location for examples upon install.
    ibraryInfo::TestsPath13The location of installed Qt testcases.
    ibraryInfo::SettingsPath100The location for Qt settings. Not applicable on Windows.

    通过 QLibraryInfo::location(QLibraryInfo::TranslationsPath) 可以获取到指定的翻译目录。

    1. QLibraryInfo支持的目录可通过 qt.conf 修改,支持值如下:
EntryDefault Value
PrefixQCoreApplication::applicationDirPath()
Documentationdoc
Headersinclude
Librarieslib
LibraryExecutableslibexec
Binariesbin
Pluginsplugins
Importsimports
Qml2Importsqml
ArchData.
Data.
Translationstranslations
Examplesexamples
Teststests
Settings.

qt.conf 默认使用资源系统中的 :/qt/etc/qt.conf,没有就依次加载当前目录 和 qt目录

[Paths]
Documentation=../../Docs/Qt-5.11.2
Examples=../../Examples/Qt-5.11.2
Prefix=..
  • 当 Qt 翻译都加载正确后, QMessage button依然没有被翻译
    Qt源码:

      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();
      }
    

我们发现 button 的文本上下文是 QPlatformTheme, 而不是 qt_zh_CN.qm 中 QDialogButtonBox
在这里插入图片描述

因此需要通过修改qt_zh_CN.ts,然后生成 qm, 如下图
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值