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.
  • K Offline
    K Offline
    KiwiJeff
    wrote on 25 May 2015, 11:12 last edited by KiwiJeff 6 Jan 2015, 15:56
    #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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 May 2015, 21:56 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

      K 1 Reply Last reply 26 May 2015, 15:48
      0
      • S SGaist
        25 May 2015, 21:56

        Hi,

        How are you changing the language ?

        K Offline
        K Offline
        KiwiJeff
        wrote on 26 May 2015, 15:48 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 27 May 2015, 18:41 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

          K 1 Reply Last reply 29 May 2015, 20:23
          0
          • S SGaist
            27 May 2015, 18:41

            How are you installing your filter object ?

            K Offline
            K Offline
            KiwiJeff
            wrote on 29 May 2015, 20:23 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 29 May 2015, 20:44 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
              • K Offline
                K Offline
                KiwiJeff
                wrote on 30 May 2015, 07:20 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
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 30 May 2015, 22:10 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
                  • K Offline
                    K Offline
                    KiwiJeff
                    wrote on 1 Jun 2015, 15:56 last edited by KiwiJeff 6 Feb 2015, 17:04
                    #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
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 1 Jun 2015, 16:03 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

                      K 1 Reply Last reply 2 Jun 2015, 17:04
                      0
                      • S SGaist
                        1 Jun 2015, 16:03

                        Didn't you forgot to declare languageNotifier ? ;)

                        K Offline
                        K Offline
                        KiwiJeff
                        wrote on 2 Jun 2015, 17:04 last edited by
                        #11

                        @SGaist Fixed it ;)

                        1 Reply Last reply
                        0

                        1/11

                        25 May 2015, 11:12

                        • Login

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