aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickdialogs/quickdialogsquickimpl/qquickfontdialogimpl.cpp
diff options
context:
space:
mode:
authorOliver Eftevaag <oliver.eftevaag@qt.io>2025-10-17 09:24:01 +0200
committerOliver Eftevaag <oliver.eftevaag@qt.io>2025-11-14 08:59:57 +0100
commit4d1753b95f0343a68f80a1b482b8ab9a0397fecb (patch)
treedd77a4d37237f0db20d3b48fba63e750699765f4 /src/quickdialogs/quickdialogsquickimpl/qquickfontdialogimpl.cpp
parentf2e3d67e32c7cbab101f1166b4eb906ecb546fde (diff)
DialogButtonBox: add properties for setting a default button
Qt Quick Controls lacked the ability for setting a default button in a dialog button box. This is something that we support in QtWidgets, and it makes sense to allow dialogs with a DialogButtonBox set as the footer to decide if a particular button should be default, and have focus when the dialog is opened. With this patch, we're introducing two different ways for selecting a default button. The first is the usage of the DialogButtonBox::defaultButton property, which works like this: ``` DialogButtonBox { defaultButton: Button { text: qsTr("Ok") } Button { text: qsTr("Cancel") } } ``` In the snippet above, the Button that is set as default should both be highlighted, and be the sub focus item of the DialogButtonBox. However, since the standardButtons property can be used to dynamically create buttons on the fly, we also needed an alternative way for picking a default button. In cases where standardButtons is being used to create buttons in the DialogButtonBox, the new defaultStandardButton property can be used: ``` DialogButtonBox { standardButtons: DialogButtonBox.Yes | DialogButtonBox.No defaultStandardButton: DialogButtonBox.Yes } ``` Something worth noting, is that the DialogButtonBox's contentItem is typically a ListView. ListView have the ItemIsFocusScope flag set, meaning that we need to give focus to both the ListView, and the default Button. In addition, the FontDialog and ColorDialog will now make the Ok button default, and give it focus when the dialogs are opened. The FileDialog and FolderDialog will continue to give the ListView focus, since that's IMO more user friendly. The MessageDialog will need new API to take full advantage of this new feature, which will be introduced later. [ChangeLog][DialogButtonBox] The DialogButtonBox now has two new properties, defaultButton and defaultStandardButton. When one of these properties are being used, a button set as default will be highlighted and receive activeFocus whenever the DialogButtonBox gets focus. If a DialogButtonBox is assigned as a Dialog's footer, it will also get focus when the Dialog is opened. This means that a Dialog with a DialogButtonBox as its footer, will give focus to a default button when opened. If both of these properties are unset, the first button with the AcceptRole will get focus, but it will not be highlighted. Fixes: QTBUG-58246 Fixes: QTBUG-139352 Change-Id: Ic083410184dd63e0e790694f782a7a98c1dc8b6e Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quickdialogs/quickdialogsquickimpl/qquickfontdialogimpl.cpp')
-rw-r--r--src/quickdialogs/quickdialogsquickimpl/qquickfontdialogimpl.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/quickdialogs/quickdialogsquickimpl/qquickfontdialogimpl.cpp b/src/quickdialogs/quickdialogsquickimpl/qquickfontdialogimpl.cpp
index 960f8785ea..6ec6382a2d 100644
--- a/src/quickdialogs/quickdialogsquickimpl/qquickfontdialogimpl.cpp
+++ b/src/quickdialogs/quickdialogsquickimpl/qquickfontdialogimpl.cpp
@@ -147,6 +147,18 @@ void QQuickFontDialogImpl::focusOutEvent(QFocusEvent *event)
attached->clearSearch();
}
+void QQuickFontDialogImpl::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data)
+{
+ Q_D(QQuickFontDialogImpl);
+ QQuickDialog::itemChange(change, data);
+
+ if (change != QQuickItem::ItemVisibleHasChanged || !isComponentComplete() || !data.boolValue)
+ return;
+
+ if (QQuickFontDialogImplAttached *attached = d->attachedOrWarn(); attached && attached->buttonBox())
+ attached->buttonBox()->forceActiveFocus(Qt::OtherFocusReason);
+}
+
QQuickFontDialogImplAttached::QQuickFontDialogImplAttached(QObject *parent)
: QObject(*(new QQuickFontDialogImplAttachedPrivate), parent),
m_writingSystem(QFontDatabase::Any),