Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QApplication::remove-/installTranslator and changeEvent()



  • In my application, I am supporting English as the main language and one or more additional languages. I am creating a new QTranslator object on the heap (with new) so that I can remove it later (saving the pointer in a std::unique_ptr type). When the user selects "English", it is removed (also deleted), and selecting other languages cause a new translator to be created which loads the appropriate *.qm file.

    The question I have concerns the changeEvent() which is called for both removeTranslator() and installTranslator(). If I am only switching between English and one other language, I get one changeEvent() call for removeTranslator() or one event call for installTranslator(). This works perfectly well.

    However, if there is already a translator installed, for example for German, and the user selects "French", I have to remove the German translator first before installing French because I get into problems if the application has more than one translator installed and English is selected. This triggers two changeEvent() calls, according to the docs, which I do not want because a lot is going on in that function, and it seems quite a waste of time to call it twice.

    Maybe I am doing this wrong?


  • Lifetime Qt Champion

    Hi,

    Normally the latest installed translator is used can't you just stack them ?



  • @SGaist said in QApplication::remove-/installTranslator and changeEvent():

    Normally the latest installed translator is used can't you just stack them ?

    Thanks for the suggestion. I don't use a dummy translation file for English, so I would have to remove all of the stack first if there were already different translators installed, and the user selects "English". Even if I did use a dummy *.qm file for English, I would have the problem with the duplicate and unnecessary changeEvents().

    Of course, it shouldn't be such a problem under normal operation because one would not expect the user to activate multiple languages during the course of one session. However, I would like to find an efficient solution to the problem. For now, I am setting a global flag which allows the changeEvent function body to return immediately if it is set.

    BTW, what happens to the memory which lives at the pointer address passed to the removeTranslator() and installTranslator() functions? Does the application assume ownership of the memory?



  • In the meantime, I am happy with the solution I found using a global variable to track the language usage, so I am marking this as SOLVED.


  • Lifetime Qt Champion

    @Robert-Hairgrove said in QApplication::remove-/installTranslator and changeEvent():

    BTW, what happens to the memory which lives at the pointer address passed to the removeTranslator() and installTranslator() functions? Does the application assume ownership of the memory?

    No it does not. When a Qt class takes ownership of something, it is documented as such.



  • @SGaist Thank you; this is good to know.


Log in to reply