qmessagebox默认显示英文的按钮,并且加载qt_zh_CN.qm翻译文件也没有什么用,而后我跟踪源码发现按钮的文字翻译是直接调用这个接口的翻译
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(); }
因此只要在程序的任意翻译文件中加入如下上下文便可显示中文按钮code
<context> <name>QPlatformTheme</name> <message> <source>OK</source> <translation>肯定</translation> </message> <message> <source>Save</source> <translation>保存</translation> </message> <message> <source>Save All</source> <translation>保存所有</translation> </message> <message> <source>Open</source> <translation>打开</translation> </message> <message> <source>&Yes</source> <translation>&是</translation> </message> <message> <source>Yes to &All</source> <translation>所有&是</translation> </message> <message> <source>&No</source> <translation>&否</translation> </message> <message> <source>N&o to All</source> <translation>&所有否</translation> </message> <message> <source>Abort</source> <translation>终止</translation> </message> <message> <source>Retry</source> <translation>重试</translation> </message> <message> <source>Ignore</source> <translation>忽略</translation> </message> <message> <source>Close</source> <translation>关闭</translation> </message> <message> <source>Cancel</source> <translation>取消</translation> </message> <message> <source>Discard</source> <translation>放弃</translation> </message> <message> <source>Help</source> <translation>帮助</translation> </message> <message> <source>Apply</source> <translation>应用</translation> </message> <message> <source>Reset</source> <translation>重置</translation> </message> <message> <source>Restore Defaults</source> <translation>恢复默认</translation> </message> </context>