如何翻译Qt中的标准按钮?



我在QMessageBox中翻译标准按钮有问题。如果我检查语言,按钮翻译得很好,但如果我不检查语言,按钮就没有翻译。每次我需要显示QMessageBox时,我如何获得翻译按钮而不检查语言?

#include "application.h"
#include "main_window.h"
#include <QTranslator>
#include <qlibraryinfo.h>
int main( int argc, char *argv[] )
{
Application application( argc, argv );
QString language = app()->settings().value("language").toString();
if (language == "Russian") // Here I check the language
{
QTranslator translator_ru;

if (translator_ru.load(QString("translations/qtbase_ru.qm")))
application.installTranslator(&translator_ru);    
if (QMessageBox::question(0, "Delete?", "First test") == QMessageBox::Yes) {} // In this message, the standard buttons are in Russian   
}
if (QMessageBox::question(0, "Delete?", "Second test") == QMessageBox::Yes) {} // In this message, the standard buttons are in English
MainWindow window;
window.show();
return application.exec();
}

我已经找到了解决方案:

#include "application.h"
#include "main_window.h"
#include <QTranslator>
#include <qlibraryinfo.h>
int main( int argc, char *argv[] )
{
Application application( argc, argv );
QString language = app()->settings().value("language").toString();
QString base_translate_file_name;
if (language == "Russian") base_translate_file_name = "qtbase_ru.qm";       
else base_translate_file_name = "qtbase_en.qm";
QTranslator qtBaseTranslator;
if (qtBaseTranslator.load(QString("translations/" + base_translate_file_name), application.applicationDirPath()))
{
application.installTranslator(&qtBaseTranslator);
}
MainWindow window;
window.show();
return application.exec();
}

相关内容

  • 没有找到相关文章

最新更新