From c18c7bd7f9596e5ad3d13876a91203e1ceba2544 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Mon, 9 Sep 2019 15:59:49 +0200 Subject: DialogButtonBox: fix standard buttons not being translated When calling QQmlEngine::retranslate() after component completion, buttons in a DialogButtonBox were not being retranslated. For now the only way to be notified of language change events is by installing an event filter on the application, but in the future we can use the solution to QTBUG-78141 instead. Change-Id: Ibc435c3829945489adcbaa8a813013fe735a9c38 Fixes: QTBUG-75085 Reviewed-by: Andy Shaw --- src/quicktemplates2/qquickdialogbuttonbox.cpp | 44 ++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'src/quicktemplates2/qquickdialogbuttonbox.cpp') diff --git a/src/quicktemplates2/qquickdialogbuttonbox.cpp b/src/quicktemplates2/qquickdialogbuttonbox.cpp index 91fb41f2..10d80778 100644 --- a/src/quicktemplates2/qquickdialogbuttonbox.cpp +++ b/src/quicktemplates2/qquickdialogbuttonbox.cpp @@ -430,7 +430,8 @@ void QQuickDialogButtonBoxPrivate::removeStandardButtons() while (i >= 0) { QQuickAbstractButton *button = qobject_cast(q->itemAt(i)); if (button) { - QQuickDialogButtonBoxAttached *attached = qobject_cast(qmlAttachedPropertiesObject(button, false)); + QQuickDialogButtonBoxAttached *attached = qobject_cast( + qmlAttachedPropertiesObject(button, false)); if (attached) { QQuickDialogButtonBoxAttachedPrivate *p = QQuickDialogButtonBoxAttachedPrivate::get(attached); if (p->standardButton != QPlatformDialogHelper::NoButton) { @@ -443,6 +444,24 @@ void QQuickDialogButtonBoxPrivate::removeStandardButtons() } } +void QQuickDialogButtonBoxPrivate::updateLanguage() +{ + Q_Q(QQuickDialogButtonBox); + int i = q->count() - 1; + while (i >= 0) { + QQuickAbstractButton *button = qobject_cast(itemAt(i)); + if (button) { + QQuickDialogButtonBoxAttached *attached = qobject_cast( + qmlAttachedPropertiesObject(button, true)); + const auto boxAttachedPrivate = QQuickDialogButtonBoxAttachedPrivate::get(attached); + const QPlatformDialogHelper::StandardButton standardButton = boxAttachedPrivate->standardButton; + const QString buttonText = QGuiApplicationPrivate::platformTheme()->standardButtonText(standardButton); + button->setText(QPlatformTheme::removeMnemonics(buttonText)); + } + --i; + } +} + QQuickDialogButtonBox::QQuickDialogButtonBox(QQuickItem *parent) : QQuickContainer(*(new QQuickDialogButtonBoxPrivate), parent) { @@ -684,11 +703,34 @@ void QQuickDialogButtonBox::updatePolish() d->updateLayout(); } +class LanguageEventFilter : public QObject +{ +public: + LanguageEventFilter(QQuickDialogButtonBoxPrivate *box) + : QObject(box->q_ptr) + , boxPrivate(box) + { + } + +protected: + bool eventFilter(QObject *, QEvent *event) + { + if (event->type() == QEvent::LanguageChange) + boxPrivate->updateLanguage(); + return false; + } + +private: + QQuickDialogButtonBoxPrivate *boxPrivate; +}; + void QQuickDialogButtonBox::componentComplete() { Q_D(QQuickDialogButtonBox); QQuickContainer::componentComplete(); d->updateLayout(); + // TODO: use the solution in QTBUG-78141 instead, when it's implemented. + qApp->installEventFilter(new LanguageEventFilter(d)); } void QQuickDialogButtonBox::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) -- cgit v1.2.3