Switching languages dynamically
So far, you have learned how to use the system language or a default language for your Qt application. In most applications, you can just detect the language in main()
and load an appropriate .qm
file. Sometimes, your application must be able to support changes to the user's language settings while still running. An application that is used by multiple people in shifts may need to switch languages without requiring a restart.
To achieve this in a Qt Widgets-based application, you can override QWidget::changeEvent()
. Then, you have to check whether the event is of the QEvent::LanguageChange
type. You can retranslate the user interface accordingly.
The following code snippet explains how to achieve dynamic translation in a Qt Widgets-based GUI:
void CustomWidget::changeEvent(QEvent *event) { Â Â Â Â if (QEvent::LanguageChange == event->type()) Â Â Â Â { Â Â Â Â Â Â Â Â ui...