QDialog content not being translated
-
I am trying to translate my application from English to German and vice versa. My application contains a mainwindow(with a menubar) and several mdi subwindows their own menu bars and there are several qdialogues, some of them are triggered via menu items within the main window and some of them are triggered via menu items within the mdi sub windows. The actual problem is that when I am triggering the translation the content of the mainwindow gets translated but no changes are reflected in any dialog that is triggered via menu item within the main window, same goes with those qdialogues that are triggered from the mdi subwindow menu items.
-
I am trying to translate my application from English to German and vice versa. My application contains a mainwindow(with a menubar) and several mdi subwindows their own menu bars and there are several qdialogues, some of them are triggered via menu items within the main window and some of them are triggered via menu items within the mdi sub windows. The actual problem is that when I am triggering the translation the content of the mainwindow gets translated but no changes are reflected in any dialog that is triggered via menu item within the main window, same goes with those qdialogues that are triggered from the mdi subwindow menu items.
hi @Jayant and welcome
if you're using the default Qt Dialogs, and you want those translated as well, than I suggest loading, besides your own translation file, also the translation file from Qt Lib/Module.You'll find those under
/Path/to/Qt/Version/(platform or compiler, for example)clang_64/translations -
Hi @J-Hilk I am not using default Qt Dialogs. I am using custom dialogs with extend the QDialog class and having a ui file.
@Jayant you may want to override the languageChange() method in your custom dialog class. Something like this (pseudo-code):
/** * Handles the event about language changed. * Retranslate all applicable strings. * */ void YourDialog::languageChange() { ui->retranslateUi(this); this->setWindowTitle(tr(Confirm Action"); // Change label of default Ok button to "Remove" ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Remove")); }
-
-
@JonB
Yes ofcourse here's my codevoid Dialog::changeEvent(QEvent *e){ if (e->type() == QEvent::LanguageChange) { ui->retranslateUi(this); setWindowTitle(tr("Test Modal Dialog")); ui->label->setText(tr("this is a label")); } else { QWidget::changeEvent(e); } }
-
@JonB
Yes ofcourse here's my codevoid Dialog::changeEvent(QEvent *e){ if (e->type() == QEvent::LanguageChange) { ui->retranslateUi(this); setWindowTitle(tr("Test Modal Dialog")); ui->label->setText(tr("this is a label")); } else { QWidget::changeEvent(e); } }
@Jayant
Well, start by verifying (that this code is hit and) what thetr("...")
s here are actually returning, rather than setting text from them.Also, I don't know, but after your code in the
LanguageChange
case, do you need to still call the baseQWidget::changeEvent(e);
to let it process? (EDIT OK, I see the examples in the docs.) And why are you callingQWidget::changeEvent()
instead ofQDialog::changeEvent()
? -
@Jayant that's obvious, as the Dialogs are created on the fly during run.
But if the translations exist, and the translation is loaded correctly, then the new created widget/dialog will also show the appropriate translated text.
You should show some more of your code, and double check that the translation really exists and is the correct one (assigned to the correct source code)
-
@Jayant that's obvious, as the Dialogs are created on the fly during run.
But if the translations exist, and the translation is loaded correctly, then the new created widget/dialog will also show the appropriate translated text.
You should show some more of your code, and double check that the translation really exists and is the correct one (assigned to the correct source code)
@J-Hilk
Translation is loaded correctly because the mainwindow shows up the translated text but the dialog class doesn't. Following code translates the textQCheckBox *checkBox = qobject_cast<QCheckBox *>(sender()); MainWindow *window = mainWindowForCheckBoxMap[checkBox]; if (!window) { QTranslator translator; translator.load(qmFileForCheckBoxMap[checkBox]); qApp->installTranslator(&translator); window = new MainWindow; window->setPalette(colorForLanguage(checkBox->text())); window->installEventFilter(this); mainWindowForCheckBoxMap.insert(checkBox, window); } window->setVisible(checkBox->isChecked());
-
@J-Hilk
Translation is loaded correctly because the mainwindow shows up the translated text but the dialog class doesn't. Following code translates the textQCheckBox *checkBox = qobject_cast<QCheckBox *>(sender()); MainWindow *window = mainWindowForCheckBoxMap[checkBox]; if (!window) { QTranslator translator; translator.load(qmFileForCheckBoxMap[checkBox]); qApp->installTranslator(&translator); window = new MainWindow; window->setPalette(colorForLanguage(checkBox->text())); window->installEventFilter(this); mainWindowForCheckBoxMap.insert(checkBox, window); } window->setVisible(checkBox->isChecked());
@Jayant
not quite what I had in mind,
Your dialogs, and how they get their textTranslation is loaded correctly because the mainwindow shows up the translated text but the dialog class doesn't. Following code translates the text
I was rather asking if you checked that the translatable strings for your dialog where in your *qm file and correctly assigned.