[Interest] Call retranslateUi() from UI generated/loaded directly from *.ui
Nicholas Yue
yue.nicholas at gmail.com
Sat May 1 02:43:27 CEST 2021
I tried enabling setLanguageChangeEnabled(true) but I don't see my label
updating, do I have to write some handler for that event and go through to
update each widget text ? Apologies, I am new to translation.
```
#include "MainWindow.h"
#include <QVBoxLayout>
#include <QDebug>
#include <QUiLoader>
#include <QFile>
#include <QWidget>
#include <QApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QUiLoader loader;
QFile file(":/designer/mainwindow.ui");
file.open(QFile::ReadOnly);
QWidget *widget = loader.load(&file, parent);
loader.setTranslationEnabled(true);
file.close();
setCentralWidget(widget);
ui_checkBox = findChild<QCheckBox*>("checkBox");
QObject::connect(ui_checkBox, SIGNAL(clicked()), this, SLOT(doCheckBox()));
}
void MainWindow::doCheckBox() {
qDebug() << "doCheckBox() called = " << ui_checkBox->isChecked();
if (ui_checkBox->isChecked())
{
QString langFile = QString(":/language/.qm/mlc_de.qm");
QTranslator translator;
if (translator.load(langFile)) {
qDebug() << "DE Successful";
bool installed = qApp->installTranslator(&translator);
if (installed) {
qDebug() << "DE installed";
}
}
else
{
qDebug() << "DE Unsuccessful";
}
}
else
{
QString langFile = QString(":/language/.qm/mlc_C.qm");
QTranslator translator;
if (translator.load(langFile)) {
qDebug() << "C Successful";
bool installed = qApp->installTranslator(&translator);
if (installed) {
qDebug() << "C installed";
}
}
else
{
qDebug() << "C Unsuccessful";
}
}
}
```
On Thu, 29 Apr 2021 at 23:39, Friedemann Kleint <Friedemann.Kleint at qt.io>
wrote:
> Hi,
>
> > I am developing an application where I am generating the UI via
> loading the *.ui rather than using uic to generate the source/header file.
>
> > I need to call retranslateUi to update my translation but most
> documentation talks about a generated method by uic
>
> It should automatically react to language change events, see:
>
> https://doc.qt.io/qt-6/quiloader.html#setLanguageChangeEnabled
>
> Regards, Friedemann
>
> --
>
> Friedemann Kleint
> The Qt Company GmbH
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
--
Nicholas Yue
Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
Custom Dev - C++ porting, OSX, Linux, Windows
http://au.linkedin.com/in/nicholasyue
https://vimeo.com/channels/naiadtools
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210430/89fd8c86/attachment.html>
More information about the Interest
mailing list