[Solved] QEvent::LanguageChange
-
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?
-
Hi,
How are you changing the language ?
-
How are you installing your filter object ?
-
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); }
-
That's not what I meant. If I understand you correctly, LanguageNotifier should work like the KeyPressEater of installEventFilter's documentation, right ?
-
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.
-
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.
-
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); }
-
Didn't you forgot to declare languageNotifier ? ;)