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. [Solved] QEvent::LanguageChange
QtWS25 Last Chance

[Solved] QEvent::LanguageChange

Scheduled Pinned Locked Moved General and Desktop
qeventqcoreapplicatioqguiapplicationqapplicationlanguagechangelocalization
11 Posts 2 Posters 8.9k 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.
  • KiwiJeffK Offline
    KiwiJeffK Offline
    KiwiJeff
    wrote on last edited by KiwiJeff
    #1

    I am working on an idea where a QObject notifies QML on language changes, so it can update itself. Sort of like how it is explained at http://wiki.qt.io/How_to_do_dynamic_translation_in_QML. But instead of emitting the signal when selecting the language, I wanted to emit it each time the global language changes.

    Now, I implemented the QEvent handler like this:

    bool LanguageNotifier::event(QEvent *event)
    {
        qDebug() << event->type();
    
        switch(event->type()) {
        case QEvent::LanguageChange:
            emit languageChanged();
            break;
        default:
            break;
        }
    
        return QObject::event(event);
    }
    

    However, what I found was that this is never called when using QGuiApplication and QObject. If I turn to QApplication and QWidget, on the other hand, the LanguageChange event is passed to the LanguageNotifier class.

    Do I need to do something else in order for a QObject to subscribe to a LanguageChange event?

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

      Hi,

      How are you changing the language ?

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

      KiwiJeffK 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        How are you changing the language ?

        KiwiJeffK Offline
        KiwiJeffK Offline
        KiwiJeff
        wrote on last edited by
        #3

        @SGaist

        Like this:

        d->m_desiredLanguage.load(QStringLiteral(":/languages/nl_NL.qm"));
        qApp->installTranslator(&d->m_desiredLanguage);
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          How are you installing your filter object ?

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

          KiwiJeffK 1 Reply Last reply
          0
          • SGaistS SGaist

            How are you installing your filter object ?

            KiwiJeffK Offline
            KiwiJeffK Offline
            KiwiJeff
            wrote on last edited by
            #5

            @SGaist

            Why would I need to install a filter object? I mean, it is not like I don't want them...

            Nevertheless, the EventFilter is never called if I install it like this:

            LanguageNotifier::LanguageNotifier(QObject *parent) :
            QObject(parent)
            {
                installEventFilter(this);
            }
            
            bool LanguageNotifier::eventFilter(QObject *obj, QEvent *event)
            {
                qDebug() << Q_FUNC_INFO << event->type();
            
                if (event->type() == QEvent::LanguageChange) {
                    return false;
                }
            
                return QObject::eventFilter(obj, event);
            }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              That's not what I meant. If I understand you correctly, LanguageNotifier should work like the KeyPressEater of installEventFilter's documentation, right ?

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

              1 Reply Last reply
              1
              • KiwiJeffK Offline
                KiwiJeffK Offline
                KiwiJeff
                wrote on last edited by
                #7

                Not necessarily. My plan is to have multiple QObjects with dedicated listener QML clients, which get notified on a global language change. From experience I know that with QWidget I can listen to the LanguageChange event, but the problem is that neither event() nor an installed eventFilter receives a LanguageChange event in a QObject implementation.

                Of course, if Qt does not distribute LanguageChange to non-widgets, that I need to implement my own language change distribution system, but what I don't understand is that I should.

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

                  We might say that QWidget is a special case. It makes sense for a QWidget to receive events related to GUI, but it wouldn't make sense for Qt to send every event to every existing QObject.

                  However you can set your LanguageNotifier as filter on QApplication/QGuiApplication and then you'll be able to handle these events the way you want.

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

                  1 Reply Last reply
                  1
                  • KiwiJeffK Offline
                    KiwiJeffK Offline
                    KiwiJeff
                    wrote on last edited by KiwiJeff
                    #9

                    Nice! That worked. Thank you.

                    For those who are interested:

                    main.cpp

                    int main(int argc, char *argv[])
                    {
                        QGuiApplication app(argc, argv);
                    
                        LanguageNotifier languageNotifier;
                        app.installEventFilter(&languageNotifier);
                        ...
                    }
                    

                    LanguageNotifier.cpp

                    bool LanguageNotifier::eventFilter(QObject *obj, QEvent *event)
                    {
                        if (event->type() == QEvent::LanguageChange) {
                            emit languageChanged();
                            return false;
                        }
                    
                        return QObject::eventFilter(obj, event);
                    }
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Didn't you forgot to declare languageNotifier ? ;)

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

                      KiwiJeffK 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Didn't you forgot to declare languageNotifier ? ;)

                        KiwiJeffK Offline
                        KiwiJeffK Offline
                        KiwiJeff
                        wrote on last edited by
                        #11

                        @SGaist Fixed it ;)

                        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