Qt World Summit: Register Today!

Dynamic Language Change

  • I couldn't fix the issue how i will fix these warnings



  • Lifetime Qt Champion

    Please post code instead screenshots.

    QLabel ctor wants a QWidget-derived class, your MyWidget does not derive from QWidget but from QObject.

  • Moderators

    @Alpkan in addition to what @Christian-Ehrlicher said, QCoreApplication does not support QWidgets, use at least QApplication instead (or QGuiApplication)

    therefore I think your pro file is probably missing important QT +=linkages too

  • @Christian-Ehrlicher

    QT -= gui
    QT += core
    QT += widgets

    I understand your point but i am trying do without qapplication

  • @Alpkan said in Dynamic Language Change:

    I understand your point but i am trying do without qapplication

    This is a non sense, if you want to use QWidget you need a QApplication or QGuiApplication

    QApplication specializes QGuiApplication with some functionality needed for QWidget-based applications. It handles widget specific initialization, finalization.

    For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. For non-QWidget based Qt applications, use QGuiApplication instead, as it does not depend on the QtWidgets library.

    Some GUI applications provide a special batch mode ie. provide command line arguments for executing tasks without manual intervention. In such non-GUI mode, it is often sufficient to instantiate a plain QCoreApplication to avoid unnecessarily initializing resources needed for a graphical user interface. The following example shows how to dynamically create an appropriate type of application instance:

    => https://doc.qt.io/qt-5/qapplication.html#details

  • so there is a code which is

    MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    titleLabel = new QLabel(this);
    okPushButton = new QPushButton(this);
    // Fire the LanguageChange event - the event handler will set the texts:
    QEvent languageChangeEvent(QEvent::LanguageChange);
    QCoreApplication::sendEvent(this, &languageChangeEvent);

    void MyWidget::changeEvent(QEvent *event)
    if (event->type() == QEvent::LanguageChange) {
    titleLabel->setText(tr("Document Title"));
    } else {
    this code is written for dynamic language change
    if is it possible use that code can u explain to me than i will do it if it is not what is true way

  • Moderators

    @Alpkan It's not, changeEvent has its root in QWidget not in QObject you can't simply name a function changeEvent in a non QWidget based class and expect it to behave like a QWidget.

    I think you want to notify your QObject based classes that a new language has been set and they should change their strings, right?

    You'll have to find a way to notify them yourself. There are countless ways, but - afaik - no ready made Qt ways

Log in to reply