diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2019-09-09 15:59:49 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2019-09-11 14:02:15 +0200 |
commit | c18c7bd7f9596e5ad3d13876a91203e1ceba2544 (patch) | |
tree | 76eea6eb3c24ae4aab5ac22925b2e71caa46c05c /src | |
parent | 1fec97053884d998442710a5d4258ffb6bed8955 (diff) |
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 <andy.shaw@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/quicktemplates2/qquickdialogbuttonbox.cpp | 44 | ||||
-rw-r--r-- | src/quicktemplates2/qquickdialogbuttonbox_p_p.h | 2 |
2 files changed, 45 insertions, 1 deletions
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<QQuickAbstractButton *>(q->itemAt(i)); if (button) { - QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(qmlAttachedPropertiesObject<QQuickDialogButtonBox>(button, false)); + QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>( + qmlAttachedPropertiesObject<QQuickDialogButtonBox>(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<QQuickAbstractButton *>(itemAt(i)); + if (button) { + QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>( + qmlAttachedPropertiesObject<QQuickDialogButtonBox>(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) diff --git a/src/quicktemplates2/qquickdialogbuttonbox_p_p.h b/src/quicktemplates2/qquickdialogbuttonbox_p_p.h index 66386911..6f9c9033 100644 --- a/src/quicktemplates2/qquickdialogbuttonbox_p_p.h +++ b/src/quicktemplates2/qquickdialogbuttonbox_p_p.h @@ -78,6 +78,8 @@ public: QQuickAbstractButton *createStandardButton(QPlatformDialogHelper::StandardButton button); void removeStandardButtons(); + void updateLanguage(); + Qt::Alignment alignment = 0; QQuickDialogButtonBox::Position position = QQuickDialogButtonBox::Footer; QPlatformDialogHelper::StandardButtons standardButtons = QPlatformDialogHelper::NoButton; |