Solved Translate Qt app
-
Hello,
I am working on Qt project , and i need to translate all the widgets of my application,for the moment translate work , but i have one question
To translate my widgets , i need to add changeEvent method to all widgets,and make a retranslateUi
the changeEvent method will contain the following code:
void MyWidget::changeEvent(QEvent *e) { QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: { ui->retranslateUi(this); break; } default: break; } }
But it seems odd to me , to add this same function in all widgets that i need to translate,
So my question is , is there any better way to inform that all widget need to be retranslated?
Am i missing something?
-
Hi,
Why is it odd ? It's a pretty simple way for the developer to know that the language changed and he can act accordingly. Not all widget have text to translate nor do they systematically need translation.
-
Hi @SGaist
Thank you for your answer,
Instead of calling changeEvent in every widget,
Is there a way to call changeEvent just on my mainWindow like this, and then translate widget that need to be translated:
void MyMainWindow::changeEvent(QEvent *e) { QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: { ui->retranslateUi(this); // Here i want to translate or my child widgets widget1->retranslateUi//or something like this widget2->retranslateUi//or something like this ... break; } default: break; } }
-
Sure you can do it like that but that would mean adding add a method to all your other widgets. However, IIRC, widgets created through designer already handle that part so there's no need for that.
Do you have other widgets that are not designer based ?
-
No,all my widgets that need to be translated are based on qt designer,
Widgets does not have
retranslateUi
method , so i don't know how to tell my widgets to be translated from my mainWindowcase QEvent::LanguageChange: { ui->retranslateUi(this); // Here i want to translate or my child widgets widget1->//looking for the function to tell widget to be translated widget2->//looking for the function to tell widget to be translated ... break; }
any idea how to do that ?
-
No it's a method implemented in the
ui
object class.If you don't want that change event in all your classes, like I wrote before, you have to add a method to each of your widget calling
ui->retranslateUi(this)
.Thus adding:
void MyWidget::changeEvent(QEvent *event) { if (event->type() == QEvent::LanguageChange) { ui->retranslateUi(this); } else QWidget::changeEvent(event); }
to your widgets would be a lot cleaner.
-
Ok , thank you for your help,
Well i think that i will reimplement the changeEvent for every widget
-
Hi All,
My application architecture demands that i cannot use the overloaded changeEvent method in all the widgets...as in my application i am using Qpushbutton which does not have the changeEvent method in it....one option is to use multiple slots for the different widgets after the changeEvent emits one signal from the base mainwidget class.....On solution i have thought is to replace the Qpushbuttton with QAbstractbutton...but that can create other problems in the architecture as the App is adding QPushbutton dynamically based on the XML file parsing.
-
QPushButton does have this method. All QWidget derived classes have that method.
I don't see why you would need to subclass QPushButton in that case. You only need to implement the changeEvent method in your complex custom widgets that will create other "base" widgets like QPushButton.
-
Got it...but now suppose i have an Item like QStandardItem in my code which will be used to show the data in a view....but how the changeEvent shall work in the above case?
-
Do you mean you are generating QStandardItem which content is translated within your code ? If that's the case can you show how you do that ?
-
Smthing like this...
Info->TreeItem = new QStandardItem();
Info->TreeItem->setText(QApplication::translate("Project Tree", Info->TreeBranch->Label->Primary) );So in the above case you dont hv a changeEvent in the QStandardItem class hence will not be able to use installtranslator() in the code
-
What would the value of Primary be ? Something fixed that is translatable or a dynamic text ?
In any case, this is typically a situation where you implement the
changeEvent
method of the class where you handle the QStandardItems.