Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickdialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
13
14#include <QtGui/qtextdocument.h>
15#include <qpa/qplatformintegration.h>
16#include <private/qguiapplication_p.h>
17
19
20/*!
21 \qmltype Dialog
22 \inherits Popup
23//! \nativetype QQuickDialog
24 \inqmlmodule QtQuick.Controls
25 \ingroup qtquickcontrols-dialogs
26 \ingroup qtquickcontrols-popups
27 \brief Popup dialog with standard buttons and a title, used for short-term interaction with the user.
28 \since 5.8
29
30 A dialog is a popup mostly used for short-term tasks and brief communications
31 with the user. Similarly to \l ApplicationWindow and \l Page, Dialog is organized
32 into three sections: \l header, \l {Popup::}{contentItem}, and \l footer.
33
34 \image qtquickcontrols-page-wireframe.webp
35 {Page wireframe with header, content area, and footer}
36
37 The \l {Popup::}{padding} properties only affect the contentItem. Use the
38 \l {Popup::}{spacing} property to affect the space between header,
39 contentItem and footer.
40
41 By default, Dialogs have \l {QQuickItem::}{focus}.
42
43 \section1 Dialog Title and Buttons
44
45 Dialog's \l title is displayed by a style-specific title bar that is assigned
46 as a dialog \l header by default.
47
48 Dialog's standard buttons are managed by a \l DialogButtonBox that is assigned
49 as a dialog \l footer by default. The dialog's \l standardButtons property is
50 forwarded to the respective property of the button box. Furthermore, the
51 \l {DialogButtonBox::}{accepted()} and \l {DialogButtonBox::}{rejected()}
52 signals of the button box are connected to the respective signals in Dialog.
53
54 \snippet qtquickcontrols-dialog.qml 1
55
56 \section1 Modal Dialogs
57
58 A \l {Popup::}{modal} dialog blocks input to other content beneath
59 the dialog. When a modal dialog is opened, the user must finish
60 interacting with the dialog and close it before they can access any
61 other content in the same window.
62
63 \snippet qtquickcontrols-dialog-modal.qml 1
64
65 \section1 Modeless Dialogs
66
67 A modeless dialog is a dialog that operates independently of other
68 content around the dialog. When a modeless dialog is opened, the user
69 is allowed to interact with both the dialog and the other content in
70 the same window.
71
72 \snippet qtquickcontrols-dialog-modeless.qml 1
73
74 \sa DialogButtonBox, {Popup Controls}
75*/
76
77/*!
78 \qmlsignal QtQuick.Controls::Dialog::accepted()
79
80 This signal is emitted when the dialog has been accepted either
81 interactively or by calling \l accept().
82
83 \note This signal is \e not emitted when closing the dialog with
84 \l {Popup::}{close()} or setting \l {Popup::}{visible} to \c false.
85
86 \sa rejected()
87*/
88
89/*!
90 \qmlsignal QtQuick.Controls::Dialog::rejected()
91
92 This signal is emitted when the dialog has been rejected either
93 interactively or by calling \l reject().
94
95 \note This signal is \e not emitted when closing the dialog with
96 \l {Popup::}{close()} or setting \l {Popup::}{visible} to \c false.
97
98 \sa accepted()
99*/
100
101/*!
102 \since QtQuick.Controls 2.3 (Qt 5.10)
103 \qmlsignal QtQuick.Controls::Dialog::applied()
104
105 This signal is emitted when the \c Dialog.Apply standard button is clicked.
106
107 \sa discarded(), reset()
108*/
109
110/*!
111 \since QtQuick.Controls 2.3 (Qt 5.10)
112 \qmlsignal QtQuick.Controls::Dialog::reset()
113
114 This signal is emitted when the \c Dialog.Reset standard button is clicked.
115
116 \sa discarded(), applied()
117*/
118
119/*!
120 \since QtQuick.Controls 2.3 (Qt 5.10)
121 \qmlsignal QtQuick.Controls::Dialog::discarded()
122
123 This signal is emitted when the \c Dialog.Discard standard button is clicked.
124
125 \sa reset(), applied()
126*/
127
128/*!
129 \since QtQuick.Controls 2.3 (Qt 5.10)
130 \qmlsignal QtQuick.Controls::Dialog::helpRequested()
131
132 This signal is emitted when the \c Dialog.Help standard button is clicked.
133
134 \sa accepted(), rejected()
135*/
136
137QPlatformDialogHelper::ButtonRole QQuickDialogPrivate::buttonRole(QQuickAbstractButton *button)
138{
139 const QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(qmlAttachedPropertiesObject<QQuickDialogButtonBox>(button, false));
140 return attached ? attached->buttonRole() : QPlatformDialogHelper::InvalidRole;
141}
142
143void QQuickDialogPrivate::handleAccept()
144{
145 Q_Q(QQuickDialog);
146 q->accept();
147}
148
149void QQuickDialogPrivate::handleReject()
150{
151 Q_Q(QQuickDialog);
152 q->reject();
153}
154
155void QQuickDialogPrivate::handleClick(QQuickAbstractButton *button)
156{
157 Q_Q(QQuickDialog);
158 switch (buttonRole(button)) {
159 case QPlatformDialogHelper::ApplyRole:
160 emit q->applied();
161 break;
162 case QPlatformDialogHelper::ResetRole:
163 emit q->reset();
164 break;
165 case QPlatformDialogHelper::DestructiveRole:
166 emit q->discarded();
167 q->close();
168 break;
169 case QPlatformDialogHelper::HelpRole:
170 emit q->helpRequested();
171 break;
172 default:
173 break;
174 }
175}
176
177// Note: Only exists for the purpose of warning users that their implicit rich text will no
178// longer work in a future version of Qt. Once this is no longer needed, it can be removed.
179bool QQuickDialogPrivate::prepareEnterTransition()
180{
181 Q_Q(QQuickDialog);
182 if (!QQuickPopupPrivate::prepareEnterTransition())
183 return false;
184
185 if (!popupWindow && !titleFormat && !titleFormatWarningDisplayed
186 && Qt::mightBeRichText(title)) {
187 QQuickTemplatesUtils::warnAboutImplicitRichText(q);
188 titleFormatWarningDisplayed = true;
189 }
190 return true;
191}
192
193QQuickDialog::QQuickDialog(QObject *parent)
194 : QQuickDialog(*(new QQuickDialogPrivate), parent)
195{
196}
197
198QQuickDialog::QQuickDialog(QQuickDialogPrivate &dd, QObject *parent)
199 : QQuickPopup(dd, parent)
200{
201 Q_D(QQuickDialog);
202
203 // Dialogs should get active focus when opened so that e.g. Cancel closes them.
204 setFocus(true);
205 QObject::connect(d->popupItem, &QQuickPopupItem::headerChanged, this, &QQuickDialog::headerChanged);
206 QObject::connect(d->popupItem, &QQuickPopupItem::footerChanged, this, &QQuickDialog::footerChanged);
207 QObject::connect(d->popupItem, &QQuickPopupItem::implicitHeaderWidthChanged, this, &QQuickDialog::implicitHeaderWidthChanged);
208 QObject::connect(d->popupItem, &QQuickPopupItem::implicitHeaderHeightChanged, this, &QQuickDialog::implicitHeaderHeightChanged);
209 QObject::connect(d->popupItem, &QQuickPopupItem::implicitFooterWidthChanged, this, &QQuickDialog::implicitFooterWidthChanged);
210 QObject::connect(d->popupItem, &QQuickPopupItem::implicitFooterHeightChanged, this, &QQuickDialog::implicitFooterHeightChanged);
211}
212
213QQuickDialog::~QQuickDialog()
214{
215 Q_D(QQuickDialog);
216 QObject::disconnect(d->popupItem, &QQuickPopupItem::headerChanged, this, &QQuickDialog::headerChanged);
217 QObject::disconnect(d->popupItem, &QQuickPopupItem::footerChanged, this, &QQuickDialog::footerChanged);
218 QObject::disconnect(d->popupItem, &QQuickPopupItem::implicitHeaderWidthChanged, this, &QQuickDialog::implicitHeaderWidthChanged);
219 QObject::disconnect(d->popupItem, &QQuickPopupItem::implicitHeaderHeightChanged, this, &QQuickDialog::implicitHeaderHeightChanged);
220 QObject::disconnect(d->popupItem, &QQuickPopupItem::implicitFooterWidthChanged, this, &QQuickDialog::implicitFooterWidthChanged);
221 QObject::disconnect(d->popupItem, &QQuickPopupItem::implicitFooterHeightChanged, this, &QQuickDialog::implicitFooterHeightChanged);
222}
223
224/*!
225 \qmlproperty string QtQuick.Controls::Dialog::title
226
227 This property holds the dialog title.
228
229 The title is displayed in the dialog header.
230
231 \code
232 Dialog {
233 title: qsTr("About")
234
235 Label {
236 text: "Lorem ipsum..."
237 }
238 }
239 \endcode
240*/
241QString QQuickDialog::title() const
242{
243 Q_D(const QQuickDialog);
244 return d->title;
245}
246
247void QQuickDialog::setTitle(const QString &title)
248{
249 Q_D(QQuickDialog);
250 if (d->title == title)
251 return;
252 d->title = title;
253
254 if (d->popupWindow)
255 d->popupWindow->setTitle(title);
256 else
257 d->popupItem->setTitle(title);
258
259 emit titleChanged();
260}
261
262/*!
263 \qmlproperty Item QtQuick.Controls::Dialog::header
264
265 This property holds the dialog header item. The header item is positioned to
266 the top, and resized to the width of the dialog. The default value is \c null.
267
268 \note Assigning a \l DialogButtonBox as a dialog header automatically connects
269 its \l {DialogButtonBox::}{accepted()} and \l {DialogButtonBox::}{rejected()}
270 signals to the respective signals in Dialog.
271
272 \note Assigning a \l DialogButtonBox, \l ToolBar, or \l TabBar as a dialog
273 header automatically sets the respective \l DialogButtonBox::position,
274 \l ToolBar::position, or \l TabBar::position property to \c Header.
275
276 \sa footer
277*/
278QQuickItem *QQuickDialog::header() const
279{
280 Q_D(const QQuickDialog);
281 return d->popupItem->header();
282}
283
284void QQuickDialog::setHeader(QQuickItem *header)
285{
286 Q_D(QQuickDialog);
287 QQuickItem *oldHeader = d->popupItem->header();
288 if (oldHeader == header)
289 return;
290
291 QQuickControlPrivate::warnIfCustomizationNotSupported(this, header, QStringLiteral("header"));
292
293 if (QQuickDialogButtonBox *buttonBox = qobject_cast<QQuickDialogButtonBox *>(oldHeader)) {
294 QObjectPrivate::disconnect(buttonBox, &QQuickDialogButtonBox::accepted, d, &QQuickDialogPrivate::handleAccept);
295 QObjectPrivate::disconnect(buttonBox, &QQuickDialogButtonBox::rejected, d, &QQuickDialogPrivate::handleReject);
296 QObjectPrivate::disconnect(buttonBox, &QQuickDialogButtonBox::clicked, d, &QQuickDialogPrivate::handleClick);
297 if (d->buttonBox == buttonBox)
298 d->buttonBox = nullptr;
299 }
300
301 if (QQuickDialogButtonBox *buttonBox = qobject_cast<QQuickDialogButtonBox *>(header)) {
302 QObjectPrivate::connect(buttonBox, &QQuickDialogButtonBox::accepted, d, &QQuickDialogPrivate::handleAccept);
303 QObjectPrivate::connect(buttonBox, &QQuickDialogButtonBox::rejected, d, &QQuickDialogPrivate::handleReject);
304 QObjectPrivate::connect(buttonBox, &QQuickDialogButtonBox::clicked, d, &QQuickDialogPrivate::handleClick);
305 d->buttonBox = buttonBox;
306 buttonBox->setStandardButtons(d->standardButtons);
307 }
308
309 d->popupItem->setHeader(header);
310}
311
312/*!
313 \qmlproperty Item QtQuick.Controls::Dialog::footer
314
315 This property holds the dialog footer item. The footer item is positioned to
316 the bottom, and resized to the width of the dialog. The default value is \c null.
317
318 \note Assigning a \l DialogButtonBox as a dialog footer automatically connects
319 its \l {DialogButtonBox::}{accepted()} and \l {DialogButtonBox::}{rejected()}
320 signals to the respective signals in Dialog.
321
322 \note Assigning a \l DialogButtonBox, \l ToolBar, or \l TabBar as a dialog
323 footer automatically sets the respective \l DialogButtonBox::position,
324 \l ToolBar::position, or \l TabBar::position property to \c Footer.
325
326 \sa header
327*/
328QQuickItem *QQuickDialog::footer() const
329{
330 Q_D(const QQuickDialog);
331 return d->popupItem->footer();
332}
333
334void QQuickDialog::setFooter(QQuickItem *footer)
335{
336 Q_D(QQuickDialog);
337 QQuickItem *oldFooter = d->popupItem->footer();
338 if (oldFooter == footer)
339 return;
340
341 QQuickControlPrivate::warnIfCustomizationNotSupported(this, footer, QStringLiteral("footer"));
342
343 if (QQuickDialogButtonBox *buttonBox = qobject_cast<QQuickDialogButtonBox *>(oldFooter)) {
344 QObjectPrivate::disconnect(buttonBox, &QQuickDialogButtonBox::accepted, d, &QQuickDialogPrivate::handleAccept);
345 QObjectPrivate::disconnect(buttonBox, &QQuickDialogButtonBox::rejected, d, &QQuickDialogPrivate::handleReject);
346 QObjectPrivate::disconnect(buttonBox, &QQuickDialogButtonBox::clicked, d, &QQuickDialogPrivate::handleClick);
347 if (d->buttonBox == buttonBox)
348 d->buttonBox = nullptr;
349 }
350 if (QQuickDialogButtonBox *buttonBox = qobject_cast<QQuickDialogButtonBox *>(footer)) {
351 QObjectPrivate::connect(buttonBox, &QQuickDialogButtonBox::accepted, d, &QQuickDialogPrivate::handleAccept);
352 QObjectPrivate::connect(buttonBox, &QQuickDialogButtonBox::rejected, d, &QQuickDialogPrivate::handleReject);
353 QObjectPrivate::connect(buttonBox, &QQuickDialogButtonBox::clicked, d, &QQuickDialogPrivate::handleClick);
354 d->buttonBox = buttonBox;
355 buttonBox->setStandardButtons(d->standardButtons);
356 }
357
358 d->popupItem->setFooter(footer);
359}
360
361/*!
362 \qmlproperty enumeration QtQuick.Controls::Dialog::standardButtons
363
364 This property holds a combination of standard buttons that are used by the dialog.
365
366 \snippet qtquickcontrols-dialog.qml 1
367
368 The buttons will be positioned in the appropriate order for the user's platform.
369
370 Possible flags:
371 \value Dialog.Ok An "OK" button defined with the \c AcceptRole.
372 \value Dialog.Open An "Open" button defined with the \c AcceptRole.
373 \value Dialog.Save A "Save" button defined with the \c AcceptRole.
374 \value Dialog.Cancel A "Cancel" button defined with the \c RejectRole.
375 \value Dialog.Close A "Close" button defined with the \c RejectRole.
376 \value Dialog.Discard A "Discard" or "Don't Save" button, depending on the platform, defined with the \c DestructiveRole.
377 \value Dialog.Apply An "Apply" button defined with the \c ApplyRole.
378 \value Dialog.Reset A "Reset" button defined with the \c ResetRole.
379 \value Dialog.RestoreDefaults A "Restore Defaults" button defined with the \c ResetRole.
380 \value Dialog.Help A "Help" button defined with the \c HelpRole.
381 \value Dialog.SaveAll A "Save All" button defined with the \c AcceptRole.
382 \value Dialog.Yes A "Yes" button defined with the \c YesRole.
383 \value Dialog.YesToAll A "Yes to All" button defined with the \c YesRole.
384 \value Dialog.No A "No" button defined with the \c NoRole.
385 \value Dialog.NoToAll A "No to All" button defined with the \c NoRole.
386 \value Dialog.Abort An "Abort" button defined with the \c RejectRole.
387 \value Dialog.Retry A "Retry" button defined with the \c AcceptRole.
388 \value Dialog.Ignore An "Ignore" button defined with the \c AcceptRole.
389 \value Dialog.NoButton An invalid button.
390
391 \sa DialogButtonBox
392*/
393QPlatformDialogHelper::StandardButtons QQuickDialog::standardButtons() const
394{
395 Q_D(const QQuickDialog);
396 return d->standardButtons;
397}
398
399void QQuickDialog::setStandardButtons(QPlatformDialogHelper::StandardButtons buttons)
400{
401 Q_D(QQuickDialog);
402 if (d->standardButtons == buttons)
403 return;
404
405 d->standardButtons = buttons;
406 if (d->buttonBox)
407 d->buttonBox->setStandardButtons(buttons);
408 emit standardButtonsChanged();
409}
410
411/*!
412 \since QtQuick.Controls 2.3 (Qt 5.10)
413 \qmlmethod AbstractButton QtQuick.Controls::Dialog::standardButton(StandardButton button)
414
415 Returns the specified standard \a button, or \c null if it does not exist.
416
417 \sa standardButtons
418*/
419QQuickAbstractButton *QQuickDialog::standardButton(QPlatformDialogHelper::StandardButton button) const
420{
421 Q_D(const QQuickDialog);
422 if (!d->buttonBox)
423 return nullptr;
424 return d->buttonBox->standardButton(button);
425}
426
427/*!
428 \qmlproperty enumeration QtQuick.Controls::Dialog::defaultStandardButton
429 \since 6.13
430
431 This property can be used together with \l standardButtons in order to make one of them default.
432 A default button will be \l {QtQuick.Controls::Button::highlighted}{highlighted},
433 and be pressed when the dialog receives an enter key event.
434
435 \sa standardButtons, DialogButtonBox::defaultStandardButton
436*/
437QPlatformDialogHelper::StandardButton QQuickDialog::defaultStandardButton() const
438{
439 Q_D(const QQuickDialog);
440 return d->defaultStandardButton;
441}
442
443void QQuickDialog::setDefaultStandardButton(QPlatformDialogHelper::StandardButton button)
444{
445 Q_D(QQuickDialog);
446 if (d->defaultStandardButton == button)
447 return;
448
449 d->defaultStandardButton = button;
450 if (d->buttonBox)
451 d->buttonBox->setDefaultStandardButton(button);
452 emit defaultStandardButtonChanged();
453}
454
455/*!
456 \since QtQuick.Controls 2.3 (Qt 5.10)
457 \qmlproperty int QtQuick.Controls::Dialog::result
458
459 This property holds the result code.
460
461 Standard result codes:
462 \value Dialog.Accepted The dialog was accepted.
463 \value Dialog.Rejected The dialog was rejected.
464
465 \sa accept(), reject(), done()
466*/
467int QQuickDialog::result() const
468{
469 Q_D(const QQuickDialog);
470 return d->result;
471}
472
473void QQuickDialog::setResult(int result)
474{
475 Q_D(QQuickDialog);
476 if (d->result == result)
477 return;
478
479 d->result = result;
480 emit resultChanged();
481}
482
483/*!
484 \since QtQuick.Controls 2.5 (Qt 5.12)
485 \qmlproperty real QtQuick.Controls::Dialog::implicitHeaderWidth
486 \readonly
487
488 This property holds the implicit header width.
489
490 The value is equal to \c {header && header.visible ? header.implicitWidth : 0}.
491
492 \sa implicitHeaderHeight, implicitFooterWidth
493*/
494qreal QQuickDialog::implicitHeaderWidth() const
495{
496 Q_D(const QQuickDialog);
497 return d->popupItem->implicitHeaderWidth();
498}
499
500/*!
501 \since QtQuick.Controls 2.5 (Qt 5.12)
502 \qmlproperty real QtQuick.Controls::Dialog::implicitHeaderHeight
503 \readonly
504
505 This property holds the implicit header height.
506
507 The value is equal to \c {header && header.visible ? header.implicitHeight : 0}.
508
509 \sa implicitHeaderWidth, implicitFooterHeight
510*/
511qreal QQuickDialog::implicitHeaderHeight() const
512{
513 Q_D(const QQuickDialog);
514 return d->popupItem->implicitHeaderHeight();
515}
516
517/*!
518 \since QtQuick.Controls 2.5 (Qt 5.12)
519 \qmlproperty real QtQuick.Controls::Dialog::implicitFooterWidth
520 \readonly
521
522 This property holds the implicit footer width.
523
524 The value is equal to \c {footer && footer.visible ? footer.implicitWidth : 0}.
525
526 \sa implicitFooterHeight, implicitHeaderWidth
527*/
528qreal QQuickDialog::implicitFooterWidth() const
529{
530 Q_D(const QQuickDialog);
531 return d->popupItem->implicitFooterWidth();
532}
533
534/*!
535 \since QtQuick.Controls 2.5 (Qt 5.12)
536 \qmlproperty real QtQuick.Controls::Dialog::implicitFooterHeight
537 \readonly
538
539 This property holds the implicit footer height.
540
541 The value is equal to \c {footer && footer.visible ? footer.implicitHeight : 0}.
542
543 \sa implicitFooterWidth, implicitHeaderHeight
544*/
545qreal QQuickDialog::implicitFooterHeight() const
546{
547 Q_D(const QQuickDialog);
548 return d->popupItem->implicitFooterHeight();
549}
550
551/*!
552 \since Qt 6.13
553 \qmlproperty enumeration QtQuick.Controls::Dialog::titleFormat
554
555 This property holds the format of the \l title.
556
557 The supported values are the same as for \l {Text::textFormat}{Text.textFormat}.
558 The default value is \c Text.AutoText.
559
560 \note This property is applied by the dialog's \l header. A custom header
561 needs to take it into account itself. It has no effect when the title is
562 displayed by a native window title bar.
563*/
564QQuickText::TextFormat QQuickDialog::titleFormat() const
565{
566 Q_D(const QQuickDialog);
567 return d->titleFormat.value_or(QQuickText::AutoText);
568}
569
570void QQuickDialog::setTitleFormat(QQuickText::TextFormat titleFormat)
571{
572 Q_D(QQuickDialog);
573 const bool changed = titleFormat != this->titleFormat();
574 d->titleFormat = titleFormat;
575 if (changed)
576 emit titleFormatChanged();
577}
578
579void QQuickDialog::resetTitleFormat()
580{
581 Q_D(QQuickDialog);
582 const bool changed = titleFormat() != QQuickText::AutoText;
583 d->titleFormat.reset();
584 if (changed)
585 emit titleFormatChanged();
586}
587
588void QQuickDialog::setOpacity(qreal opacity)
589{
590 Q_D(QQuickDialog);
591 QQuickPopup::setOpacity(d->popupWindow ? qreal(!qFuzzyIsNull(opacity)) : opacity);
592}
593
594/*!
595 \qmlmethod void QtQuick.Controls::Dialog::accept()
596
597 Emits the \l accepted() signal and closes the dialog.
598
599 \sa reject(), done()
600*/
601void QQuickDialog::accept()
602{
603 done(Accepted);
604}
605
606/*!
607 \qmlmethod void QtQuick.Controls::Dialog::reject()
608
609 Emits the \l rejected() signal and closes the dialog.
610
611 \sa accept(), done()
612*/
613void QQuickDialog::reject()
614{
615 done(Rejected);
616}
617
618/*!
619 \since QtQuick.Controls 2.3 (Qt 5.10)
620 \qmlmethod void QtQuick.Controls::Dialog::done(int result)
621
622 \list 1
623 \li Sets the \a result.
624 \li Emits \l accepted() or \l rejected() depending on
625 whether the result is \c Dialog.Accepted or \c Dialog.Rejected,
626 respectively.
627 \li Emits \l{Popup::}{closed()}.
628 \endlist
629
630 \sa accept(), reject(), result
631*/
632void QQuickDialog::done(int result)
633{
634 setResult(result);
635
636 if (result == Accepted)
637 emit accepted();
638 else if (result == Rejected)
639 emit rejected();
640
641 close();
642}
643
644void QQuickDialog::keyPressEvent(QKeyEvent *event)
645{
646 Q_D(QQuickDialog);
647 if ((!event->modifiers() && event->key() == Qt::Key_Return)
648 || (event->modifiers() & Qt::KeypadModifier && event->key() == Qt::Key_Enter)) {
649 if (d->buttonBox) {
650 QQuickAbstractButton *effectiveDefaultButton = nullptr;
651
652 auto *buttonBoxPrivate = QQuickDialogButtonBoxPrivate::get(d->buttonBox);
653
654 // Try the first default button.
655 int standardButtonIndex = buttonBoxPrivate->indexOfDefaultStandardButton();
656 if (auto *standardButton = qobject_cast<QQuickAbstractButton *>(d->buttonBox->itemAt(standardButtonIndex));
657 standardButton && standardButton->isVisible() && standardButton->isEnabled())
658 effectiveDefaultButton = standardButton;
659
660 // If there is none, try first button with accept role.
661 if (!effectiveDefaultButton) {
662 int acceptButtonIndex = buttonBoxPrivate->indexOfAcceptButton();
663 if (auto *defaultButton = qobject_cast<QQuickAbstractButton *>(d->buttonBox->itemAt(acceptButtonIndex));
664 defaultButton && defaultButton->isVisible() && defaultButton->isEnabled())
665 effectiveDefaultButton = defaultButton;
666 }
667
668 if (effectiveDefaultButton) {
669 effectiveDefaultButton->animateClick();
670 event->accept();
671 return;
672 }
673 }
674 }
675
676 QQuickPopup::keyPressEvent(event);
677}
678
679void QQuickDialog::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data)
680{
681 QQuickPopup::itemChange(change, data);
682
683 if (change != QQuickItem::ItemVisibleHasChanged || !isComponentComplete() || !data.boolValue)
684 return;
685
686 Q_D(QQuickDialog);
687 if (d->buttonBox && QQuickItemPrivate::get(d->popupItem)->subFocusItem == nullptr) {
688 for (int i = 0; i < d->buttonBox->count(); ++i) {
689 QQuickItem *item = qobject_cast<QQuickItem *>(d->buttonBox->itemAt(i));
690 if (item && item->isEnabled()) {
691 d->buttonBox->setFocus(true, Qt::OtherFocusReason);
692 break;
693 }
694 }
695 }
696}
697
698#if QT_CONFIG(accessibility)
699QAccessible::Role QQuickDialog::accessibleRole() const
700{
701 return QAccessible::Dialog;
702}
703
704void QQuickDialog::accessibilityActiveChanged(bool active)
705{
706 Q_D(QQuickDialog);
707 QQuickPopup::accessibilityActiveChanged(active);
708
709 if (active)
710 maybeSetAccessibleName(d->popupItem->title());
711}
712#endif
713
714QT_END_NAMESPACE
715
716#include "moc_qquickdialog_p.cpp"
Combined button and popup list for selecting options.