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

356 Views Asked by At

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 :

  1. 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.

  2. 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.

2

There are 2 best solutions below

0
On

I don't see a problem as the text inside labels/tool-tips etc. is easy to change in Qt. Simply call QLabel::setText and QToolTip::showText methods with appropriate text from your class methods, i.e.:

myTruck::myTruck(QWidget *parent) :
   QObject(parent)
{
   /* do whatever you do in your class constructor */
   <...>
   myLabel->setText("Truck");
   <...>
}

myTruck::~myTruck() :
   QObject(parent)
{
   /* do whatever you do in your class destructor */
   <...>
   myTooltip->showText("Truck deleted successfully");
   <...>
}

myLabel and myTooltip probably are members of MainWindow and in case you do not have direct access to them (private members), signal&slots mechanism may be required to change them.

2
On

A file/tab will probably know what it manages ("Truck", "Car", "Airplane"), so it will need an id to make this distiction

For the tooltips: Subscribe to the changes of the 'current' file/tab. A QTabWidget has a signal void currentChanged(int index) to which you can subscribe. When the current file/tab changes (either because a new file is opened and gets focus or because the user tabbed to another file/tab), retrieve the id from the new current file/tab and set the text of the tooltips appropriately.

For the messageboxes: When you want to display a messagebox for "... deleted successfully", retrieve the id of the current file/tab and use this to construct the message you need to show.