Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTranslator loaded but no translation is done for QWidget object.
Qt 6.11 is out! See what's new in the release blog

QTranslator loaded but no translation is done for QWidget object.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 791 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GPX_Pli
    wrote on last edited by
    #1

    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);
    }
    
    --------------------------------------------
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved