PDA

View Full Version : How to replace UI labels/tool-tips in Qt application run-time according to context?



anwar.qt
5th February 2013, 14:32
We are developing a Qt Desktop application. Application can open multiple files one in each tab, and each tab (file) has its own context. User can open multiple files and switch among these tabs.

e.g. : Like one tab is of Truck management for which action (add_Vehicle) in tool bar should display tool-tip "Add Truck", and if another tab is of Car Management same action should display tool-tip "Add Car".

There are many things like this :



If I delete truck (i.e. Vehicle from Truck management tab) it should show message box "Truck deleted successfully." and if car (i.e. Vehicle from Car management tab) is deleted "Car deleted successfully" message should be shown.


There are some dialogs in which texts are directly set to the QLabels through QtDesinger.



How This problem is arised?

Previously the application is supporting only Truck management, Now we are adding support for car management in same application. All messages/UI labels/ Tool-tips are using "Truck". Now We want to change it according to the tab (file) opened.

Feasible thought solution :(Not working at run-time)

All UILabels/ToolTips/Messages are translatable [i.e. written with QObject::tr(..)]. We thought of installing QTranslator according to tab context which will automatically change the texts at all places. Installing QTranslator run-time is not working.

Is there another way of doing this? Or any pointer/hints will be very helpful.

wysota
5th February 2013, 14:38
There are different solutions available depending on what classes you use. But essentially everything boils down to either having separate actions (with separate tooltips and status bar tips) for each "context" or to setting tips manually based on the "context" when the "context" changes (e.g. when you switch tabs).

anwar.qt
5th February 2013, 14:50
I tried to install QTranslator dynamically (run-time) but it didn't worked, I might have done something wrong.
can't we install QTranslator dynamically (run-time)??

wysota
5th February 2013, 15:09
You can install a translator but it won't help you in this situation.

anwar.qt
5th February 2013, 15:25
I think it will work if it can translate UILabels/Messages dynamically.
I was thinking to create two translation file "application_Truck_en" (all texts in context of Truck) and "appication_Car_en" (all text in context of car).
On tab switch loading file with appropriate context and installing QTranslator may serve it. (if it translates all texts accordingly at that instant)

But translation of text is not working at run-time apparently (I might be doing something wrong).

What do you think of it? Is it feasible to work this way?

wysota
5th February 2013, 17:36
I think it will work if it can translate UILabels/Messages dynamically.
I was thinking to create two translation file "application_Truck_en" (all texts in context of Truck) and "appication_Car_en" (all text in context of car).
On tab switch loading file with appropriate context and installing QTranslator may serve it. (if it translates all texts accordingly at that instant)

But translation of text is not working at run-time apparently (I might be doing something wrong).

What do you think of it? Is it feasible to work this way?

The translator doesn't do any magic. You'll have more work switching translations than benefits from using a translator. And what if some day you will really need to translate your application to a different language? The proper way is really to choose from what I offered you. It's not more work than abusing the translation mechanism.

anwar.qt
6th February 2013, 08:28
NO I wasn't abusing Translation mechanism, in fact I think its wonderful. I was thinking of it as a easier solution to this problem,but as it turns out its not. Now I will think of some other solution.

Thank you very much for your help. :)