How can I translate the OK and Cancel buttons when using the static QMessageBox functions?

The OK and Cancel text for the buttons is part of the Qt source code. For more information on how to translate the Qt strings into your own language then look at lconvert. All you need to do is load the Qt translation file in addition to your original .qm file(s) when you load up your translation file(s). For the OK and Cancel buttons you would need to translate their text in the QDialogButtonBox section.

The example below demonstrates how this can be done:

TEMPLATE = app
SOURCES += main.cpp
TRANSLATIONS = t1_fr.ts qt_fr.ts
CONFIG += console

  1. #include <QtGui>
  2.  
  3. int main(int argc, char **argv)
  4. {
  5.     QApplication a(argc, argv);
  6.     QTranslator trans(0);
  7.     trans.load("t1_fr", ".");
  8.     QTranslator trans2(0);
  9.     trans2.load("qt_fr", ".");
  10.     a.installTranslator(&trans);
  11.     a.installTranslator(&trans2);
  12.     QMessageBox::warning(0, QObject::tr("Warning Title"),
  13.                          QObject::tr("Warning Message"),
  14.                          QMessageBox::Ok|QMessageBox::Cancel);
  15.     return a.exec();
  16. }
  17.  

No comments

Write a comment

Sorry, you must be logged in to post a comment.