如何在QT中翻译标准按钮?

发布于 2025-01-26 11:30:29 字数 982 浏览 2 评论 0原文

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

I have a problem with translate standard buttons in QMessageBox. If I check the language, the buttons are translated very well, but if I don't check the language, the buttons are not translated. How can I get translated buttons without checking the language every time when I need to show 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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2025-02-02 11:30:29

我找到了解决方案:

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

I've found the solution:

#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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文