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>