summaryrefslogtreecommitdiffstats
path: root/doc/src/snippets
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2023-01-31 14:19:35 +0100
committerFabian Kosmale <fabian.kosmale@qt.io>2023-02-08 10:41:29 +0100
commit98504f2c93599c62e5fac7f4b601bd8d04073fba (patch)
treef5bee4ea7274740fccbe4de403881aa60bc0583e /doc/src/snippets
parent545745ad7acf4b0a7025bd1c231a4515df60c813 (diff)
qml/i18n: Mention LanguageChange event for C++ types
When QQmlEngine::retranslate refreshed all bindings, that conveniently worked also for C++ properties. However, that is no longer the case since Qt 6.2, which only refreshes actual translation bindings. As bindings cannot know that a C++ setter uses a translation macro/function, there needs to be a way to inform the engine about them. Promote event handling as the recommended approach in C++ types, which aligns with what needs to be done in a Widgets application. Task-number: QTBUG-102393 Pick-to: 6.5 Change-Id: I16b91fcea35331d300fd70756de8819740b5dcb4 Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'doc/src/snippets')
-rw-r--r--doc/src/snippets/code/doc_src_i18n.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/doc/src/snippets/code/doc_src_i18n.cpp b/doc/src/snippets/code/doc_src_i18n.cpp
index a258d4750..717c387fd 100644
--- a/doc/src/snippets/code/doc_src_i18n.cpp
+++ b/doc/src/snippets/code/doc_src_i18n.cpp
@@ -136,3 +136,55 @@ void same_global_function(LoginWidget *logwid)
app.installTranslator(&qtTranslator);
}
//! [14]
+
+//! [15]
+
+class MyItem : public QQuickItem
+{
+ Q_OJBECT
+ QML_ELEMENT
+
+ Q_PROPERTY(QString greeting READ greeting NOTIFY greetingChanged)
+
+public signals:
+ void greetingChanged();
+public:
+ QString greeting() const
+ {
+ return tr("Hello World!");
+ }
+
+ bool event(QEvent *ev) override
+ {
+ if (ev->type() == QEvent::LanguageChange)
+ emit greetingChanged();
+ return QQuickItem::event(ev);
+ }
+};
+//! [15]
+
+
+//! [16]
+class CustomObject : public QObject
+{
+ Q_OBJECT
+
+public:
+ QList<QQuickItem *> managedItems;
+
+ CustomObject(QOject *parent = nullptr) : QObject(parent)
+ {
+ QCoreApplication::instance()->installEventFilter(this);
+ }
+
+ bool eventFilter(QObject *obj, QEvent *ev) override
+ {
+ if (obj == QCoreApplication::instance() && ev->type() == QEvent::LanguageChange) {
+ for (auto item : std::as_const(managedItems))
+ QCoreApplication::sendEvent(item, ev);
+ // do any further work on reaction, e.g. emit changed signals
+ }
+ return false;
+ }
+};
+//! [16]