Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QApplication::remove-/installTranslator and changeEvent()
Forum Update on Monday, May 27th 2025

QApplication::remove-/installTranslator and changeEvent()

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 638 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

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

        R Offline
        R Offline
        Robert Hairgrove
        wrote on last edited by
        #3

        @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?

        SGaistS 1 Reply Last reply
        0
        • R Offline
          R Offline
          Robert Hairgrove
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • R Robert Hairgrove

            @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?

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 1 Reply Last reply
            1
            • SGaistS SGaist

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

              R Offline
              R Offline
              Robert Hairgrove
              wrote on last edited by
              #6

              @SGaist Thank you; this is good to know.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved