ruhig brauner
ruhig brauner

Reputation: 963

Qt Translator doesn't work in Lambda Slot

I try to convince my Qt app to translate the buttons in a dummy dialog. For unknown reason, the first dialog translates to german but the dialog executed in the lambda slot doesn't translate. I already tried to use the older signal syntax to see if the lambda expression was the issue, but it didn't change the outcome. After the RTWindow constructor is finished, the application constructor is done and the main() calls app.exec(). There is no code I have control over that might replaced the translator.

In this code, the callback called by clicking on the menu item creates the untranslated dialog. The dialog created in the constructor is translated.

I don't see

RTWindow::RTWindow(QWidget *parent) : QMainWindow(parent)
{
ui.setupUi(this);



this->setWindowTitle("RodeTracker 2");

auto fileMenu       = new QMenu("Datei");
auto configMenu     = new QMenu("Bearbeiten");
auto clientAction   = new QAction("Kunden ...");

configMenu->addAction(clientAction);

ui.menuBar->addMenu(fileMenu);
ui.menuBar->addMenu(configMenu);

// translates fine:
QMessageBox msgBox;
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel);
msgBox.exec();

connect(clientAction, &QAction::triggered, [=]()
{
    // doesn't translate:
    QMessageBox msgBox;
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel); 
    msgBox.setDefaultButton(QMessageBox::Cancel);
    msgBox.exec();
});

}

The translation is installed with those lines: (QApplication derivative constructor)

QLocale::setDefault(QLocale(QLocale::German, QLocale::Germany));

QTranslator qtBaseTranslator;
if (qtBaseTranslator.load(QLocale::German, "qtbase", "_", "")) 
{
    installTranslator(&qtBaseTranslator);
    qDebug() << "Base Translator loaded";
}

I get the "Base Translator loaded" output. Does anybody know, what might mess with the translation?

Upvotes: 0

Views: 249

Answers (1)

ruhig brauner
ruhig brauner

Reputation: 963

The QTranslator object needs to exist during the full lifespan of the QApplication. In the example above, generating a translator as a local variable in QAppDeriv's constructor will be fine during the construction but not after that. The correct approach would be to create the QTranslator on the heap.

QAppDeriv::QAppDeriv(int &argc, char **argv, int flags)
    : QApplication(argc, argv, flags)

{
    // set default local
    QLocale::setDefault(QLocale(QLocale::German, QLocale::Germany));

    // create translator objects on heap
    QTranslator *qtTranslator = new QTranslator();
    if (qtTranslator->load(QLocale::German, "qt", "_", ""))
    {
        installTranslator(qtTranslator);
    }

    QTranslator *qtBaseTranslator = new QTranslator();
    if (qtBaseTranslator->load(QLocale::German, "qtbase", "_", ""))
    {
        installTranslator(qtBaseTranslator);
    }

    // create the main window
    window  = std::make_shared<RTWindow>();
    window->show();

}

Upvotes: 0

Related Questions