QTranslator loaded but no translation is done for QWidget object.
Unsolved
General and Desktop
-
hi
I am learning a bit on the translation of the application. I managed to do it with UI designer, but as to those not-UI object, I couldn't translation the text, like the label or button and so on. But the simple code I wrote can translate the content in the QMessageBox. So could someone have a look and tell me the problem.? and I still not don't know what is the difference between UI object and QWidget object when doing language translation.Thanks!!
.pro file -------------------------------------------- TEMPLATE = app TARGET = lang QT += core gui greaterThan(QT_MAJOR_VERSION, 4) : QT += widgets TRANSLATIONS = se.ts HEADERS += \ mainwindow.h SOURCES += \ mainwindow.cpp \ main.cpp -------------------------------------------- .h file -------------------------------------------- #include <QMainWindow> #include <QTranslator> #include <QPushButton> #include <QMessageBox> class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(); private slots: void translate(); void clickMe(); void changeEvent(QEvent *event); private: QPushButton *m_button; QPushButton *m_button1; QTranslator * m_translator; }; #endif // MAINWINDOW_H -------------------------------------------- .cpp file -------------------------------------------- #include "mainwindow.h" #include <QApplication> #include <QDebug> MainWindow::MainWindow() { m_button = new QPushButton(QObject::tr("clike me"), this); m_button1 = new QPushButton(QObject::tr("change lang"), this); m_button->setGeometry(60, 50, 100, 30); connect(m_button, SIGNAL(clicked()), this, SLOT(clickMe())); connect(m_button1, SIGNAL(clicked()), this, SLOT(translate())); } void MainWindow::clickMe() { QMessageBox::information(this, QObject::tr("This is the title"), QObject::tr("happy birthday"),QMessageBox::Yes); } void MainWindow::translate() { m_translator = new QTranslator; if (m_translator->load("se", "/Users/xxlanginsweden/Documents/QT/lang")) { qDebug() << "Load ok"; } else { qDebug() << "Not ok"; } qApp->installTranslator(m_translator); } void MainWindow::changeEvent(QEvent *event) { if (event->type() == QEvent::LanguageChange) QMainWindow::changeEvent(event); } --------------------------------------------
-
Hi and welcome to devnet,
You can see how it's done in the Dynamic Translation part of the internationalization doc.
The method described there is done for you when you use a designed built widget.