53 if (qobject_cast<
const QColorDialog *>(dialog))
54 return QPlatformTheme::ColorDialog;
56#if QT_CONFIG(fontdialog)
57 if (qobject_cast<
const QFontDialog *>(dialog))
58 return QPlatformTheme::FontDialog;
60#if QT_CONFIG(messagebox)
61 if (qobject_cast<
const QMessageBox *>(dialog))
62 return QPlatformTheme::MessageDialog;
64#if QT_CONFIG(errormessage)
65 if (qobject_cast<
const QErrorMessage *>(dialog))
66 return QPlatformTheme::MessageDialog;
68#if !QT_CONFIG(filedialog) && !QT_CONFIG(colordialog) && !QT_CONFIG(fontdialog) &&
69 !QT_CONFIG(messagebox) && !QT_CONFIG(errormessage)
75QDialogPrivate::~QDialogPrivate()
77 delete m_platformHelper;
80QPlatformDialogHelper *QDialogPrivate::platformHelper()
const
84 if (!m_platformHelperCreated && canBeNativeDialog()) {
85 m_platformHelperCreated =
true;
86 QDialogPrivate *ncThis =
const_cast<QDialogPrivate *>(
this);
87 QDialog *dialog = ncThis->q_func();
88 const int type = themeDialogType(dialog);
90 m_platformHelper = QGuiApplicationPrivate::platformTheme()
91 ->createPlatformDialogHelper(
static_cast<QPlatformTheme::DialogType>(type));
92 if (m_platformHelper) {
93 QObject::connect(m_platformHelper, SIGNAL(accept()), dialog, SLOT(accept()));
94 QObject::connect(m_platformHelper, SIGNAL(reject()), dialog, SLOT(reject()));
95 ncThis->initHelper(m_platformHelper);
99 return m_platformHelper;
102bool QDialogPrivate::canBeNativeDialog()
const
104 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs))
107 QDialogPrivate *ncThis =
const_cast<QDialogPrivate *>(
this);
108 QDialog *dialog = ncThis->q_func();
109 const int type = themeDialogType(dialog);
111 return QGuiApplicationPrivate::platformTheme()
112 ->usePlatformNativeDialog(
static_cast<QPlatformTheme::DialogType>(type));
117
118
119
120
121void QDialogPrivate::close(
int resultCode)
125 q->setResult(resultCode);
127 if (!data.is_closing) {
131 struct CloseEventEater : QObject
133 using QObject::QObject;
135 bool eventFilter(QObject *o, QEvent *e) override
137 if (e->type() == QEvent::Close)
139 return QObject::eventFilter(o, e);
142 q->installEventFilter(&closeEventEater);
143 QWidgetPrivate::close();
152 resetModalitySetByOpen();
155void QDialogPrivate::recreate()
160 QPointer<QEventLoop> existingEventLoop = std::exchange(eventLoop,
nullptr);
161 QWidgetPrivate::recreate();
162 eventLoop = existingEventLoop;
165QWindow *QDialogPrivate::transientParentWindow()
const
168 if (
const QWidget *parent = q->nativeParentWidget())
169 return parent->windowHandle();
170 else if (q->windowHandle())
171 return q->windowHandle()->transientParent();
175bool QDialogPrivate::setNativeDialogVisible(
bool visible)
177 if (QPlatformDialogHelper *helper = platformHelper()) {
180 helperPrepareShow(helper);
181 nativeDialogInUse = helper->show(q->windowFlags(), q->windowModality(), transientParentWindow());
182 }
else if (nativeDialogInUse) {
186 return nativeDialogInUse;
189QVariant QDialogPrivate::styleHint(QPlatformDialogHelper::StyleHint hint)
const
191 if (
const QPlatformDialogHelper *helper = platformHelper())
192 return helper->styleHint(hint);
193 return QPlatformDialogHelper::defaultStyleHint(hint);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
357
358
359
360
361
362
365
366
367
368
369
370
374
375
376
377
378
379
380
381
382
383
384
385
387QDialog::QDialog(QWidget *parent, Qt::WindowFlags f)
388 : QWidget(*
new QDialogPrivate, parent,
389 f | ((f & Qt::WindowType_Mask) == 0 ? Qt::Dialog : Qt::WindowType(0)))
394
395
396
397QDialog::QDialog(QDialogPrivate &dd, QWidget *parent, Qt::WindowFlags f)
398 : QWidget(dd, parent, f | ((f & Qt::WindowType_Mask) == 0 ? Qt::Dialog : Qt::WindowType(0)))
403
404
418
419
420
421
422
423
424#if QT_CONFIG(pushbutton)
425void QDialogPrivate::setDefault(QPushButton *pushButton)
428 bool hasMain =
false;
429 QList<QPushButton*> list = q->findChildren<QPushButton*>();
430 for (
int i=0; i<list.size(); ++i) {
431 QPushButton *pb = list.at(i);
432 if (pb->window() == q) {
435 if (pb != pushButton)
436 pb->setDefault(
false);
439 if (!pushButton && hasMain)
440 mainDef->setDefault(
true);
442 mainDef = pushButton;
446
447
448
449
450void QDialogPrivate::setMainDefault(QPushButton *pushButton)
453 setDefault(pushButton);
457
458
459
460
461void QDialogPrivate::hideDefault()
464 QList<QPushButton*> list = q->findChildren<QPushButton*>();
465 for (
int i=0; i<list.size(); ++i) {
466 list.at(i)->setDefault(
false);
471void QDialogPrivate::resetModalitySetByOpen()
474 if (resetModalityTo != -1 && !q->testAttribute(Qt::WA_SetWindowModality)) {
476 q->setWindowModality(Qt::WindowModality(resetModalityTo));
477 q->setAttribute(Qt::WA_SetWindowModality, wasModalitySet);
479 Q_ASSERT(resetModalityTo != Qt::WindowModal);
480 q->setParent(q->parentWidget(), Qt::Dialog);
483 resetModalityTo = -1;
487
488
489
490
491
492
493
494
495
496int QDialog::result()
const
503
504
505
506
507
508
509
510void QDialog::setResult(
int r)
517
518
519
520
521
526 Qt::WindowModality modality = windowModality();
527 if (modality != Qt::WindowModal) {
528 d->resetModalityTo = modality;
529 d->wasModalitySet = testAttribute(Qt::WA_SetWindowModality);
530 setWindowModality(Qt::WindowModal);
531 setAttribute(Qt::WA_SetWindowModality,
false);
533 setParent(parentWidget(), Qt::Sheet);
566 if (Q_UNLIKELY(d->eventLoop)) {
567 qWarning(
"QDialog::exec: Recursive call detected");
571 bool deleteOnClose = testAttribute(Qt::WA_DeleteOnClose);
572 setAttribute(Qt::WA_DeleteOnClose,
false);
574 d->resetModalitySetByOpen();
576 bool wasShowModal = testAttribute(Qt::WA_ShowModal);
577 setAttribute(Qt::WA_ShowModal,
true);
582 QPointer<QDialog> guard =
this;
583 if (d->nativeDialogInUse) {
584 d->platformHelper()->exec();
586 QEventLoop eventLoop;
587 d->eventLoop = &eventLoop;
588 (
void) eventLoop.exec(QEventLoop::DialogExec);
591 return QDialog::Rejected;
592 d->eventLoop =
nullptr;
594 setAttribute(Qt::WA_ShowModal, wasShowModal);
597 if (d->nativeDialogInUse)
598 d->helperDone(
static_cast<QDialog::DialogCode>(res), d->platformHelper());
675void QDialog::contextMenuEvent(QContextMenuEvent *e)
677#if !QT_CONFIG(whatsthis) || !QT_CONFIG(menu)
680 QWidget *w = childAt(e->pos());
682 w = rect().contains(e->pos()) ?
this :
nullptr;
686 while (w && w->whatsThis().size() == 0 && !w->testAttribute(Qt::WA_CustomWhatsThis))
687 w = w->isWindow() ?
nullptr : w->parentWidget();
689 QPointer<QMenu> p =
new QMenu(
this);
690 QAction *wt = p.data()->addAction(tr(
"What's This?"));
691 if (p.data()->exec(e->globalPos()) == wt) {
692 QHelpEvent e(QEvent::WhatsThis, w->rect().center(),
693 w->mapToGlobal(w->rect().center()));
694 QCoreApplication::sendEvent(w, &e);
703void QDialog::keyPressEvent(QKeyEvent *e)
705#ifndef QT_NO_SHORTCUT
709 if (e->matches(QKeySequence::Cancel)) {
713 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {
715#if QT_CONFIG(pushbutton)
717 case Qt::Key_Return: {
718 QList<QPushButton*> list = findChildren<QPushButton*>();
719 for (
int i=0; i<list.size(); ++i) {
720 QPushButton *pb = list.at(i);
721 if (pb->isDefault() && pb->isVisible()) {
768void QDialogPrivate::setVisible(
bool visible)
771 if (!q->testAttribute(Qt::WA_DontShowOnScreen) && canBeNativeDialog() && setNativeDialogVisible(visible))
777 const bool dontBlockWindows = q->testAttribute(Qt::WA_DontShowOnScreen)
778 && styleHint(QPlatformDialogHelper::DialogIsQtWindow).toBool();
779 Qt::WindowModality oldModality;
782 if (dontBlockWindows) {
783 oldModality = q->windowModality();
784 wasModalitySet = q->testAttribute(Qt::WA_SetWindowModality);
785 q->setWindowModality(Qt::NonModal);
789 QWidgetPrivate::setVisible(visible);
793 if (!q->testAttribute(Qt::WA_ShowWithoutActivating)) {
794 QWidget *fw = q->window()->focusWidget();
799
800
801
802
803
804
805
806
807
808#if QT_CONFIG(pushbutton)
809 if (mainDef && fw->focusPolicy() == Qt::NoFocus) {
811 while ((first = first->nextInFocusChain()) != fw && first->focusPolicy() == Qt::NoFocus)
813 if (first != mainDef && qobject_cast<QPushButton*>(first))
816 if (!mainDef && q->isWindow()) {
818 while ((w = w->nextInFocusChain()) != fw) {
819 QPushButton *pb = qobject_cast<QPushButton *>(w);
820 if (pb && pb->autoDefault() && pb->focusPolicy() != Qt::NoFocus) {
821 pb->setDefault(
true);
827 if (fw && !fw->hasFocus()) {
828 QFocusEvent e(QEvent::FocusIn, Qt::TabFocusReason);
829 QCoreApplication::sendEvent(fw, &e);
833#if QT_CONFIG(accessibility)
834 QAccessibleEvent event(q, QAccessible::DialogStart);
835 QAccessible::updateAccessibility(&event);
840#if QT_CONFIG(accessibility)
841 if (q->isVisible()) {
842 QAccessibleEvent event(q, QAccessible::DialogEnd);
843 QAccessible::updateAccessibility(&event);
848 QWidgetPrivate::setVisible(visible);
853 if (dontBlockWindows) {
854 q->setWindowModality(oldModality);
855 q->setAttribute(Qt::WA_SetWindowModality, wasModalitySet);
858#if QT_CONFIG(pushbutton)
859 const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
860 if (mainDef && q->isActiveWindow()
861 && theme->themeHint(QPlatformTheme::DialogSnapToDefaultButton).toBool())
862 QCursor::setPos(mainDef->mapToGlobal(mainDef->rect().center()));
867void QDialog::showEvent(QShowEvent *event)
869 if (!event->spontaneous() && !testAttribute(Qt::WA_Moved)) {
870 Qt::WindowStates state = windowState();
871 adjustPosition(parentWidget());
872 setAttribute(Qt::WA_Moved,
false);
873 if (state != windowState())
874 setWindowState(state);
879void QDialog::adjustPosition(QWidget* w)
883 if (
const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
884 if (theme->themeHint(QPlatformTheme::WindowAutoPlacement).toBool())
887 int extraw = 0, extrah = 0;
888 const QWindow *parentWindow =
nullptr;
892 parentWindow = d->transientParentWindow();
895 QScreen *scrn =
nullptr;
898 else if (parentWindow)
899 scrn = parentWindow->screen();
900 else if (QGuiApplication::primaryScreen()->virtualSiblings().size() > 1)
901 scrn = QGuiApplication::screenAt(QCursor::pos());
905 desk = scrn->availableGeometry();
907 QWidgetList list = QApplication::topLevelWidgets();
908 for (
int i = 0; (extraw == 0 || extrah == 0) && i < list.size(); ++i) {
909 QWidget * current = list.at(i);
910 if (current->isVisible()) {
911 int framew = current->geometry().x() - current->x();
912 int frameh = current->geometry().y() - current->y();
914 extraw = qMax(extraw, framew);
915 extrah = qMax(extrah, frameh);
921 if (extraw == 0 || extrah == 0 || extraw >= 10 || extrah >= 40) {
930 if (w->windowHandle() && qvariant_cast<WId>(w->windowHandle()->property(
"_q_embedded_native_parent_handle")))
933 pp = w->mapToGlobal(QPoint(0,0));
934 p = QPoint(pp.x() + w->width()/2,
935 pp.y() + w->height()/ 2);
936 }
else if (parentWindow) {
939 QPoint pp = parentWindow->mapToGlobal(QPoint(0, 0));
940 p = QPoint(pp.x() + parentWindow->width() / 2, pp.y() + parentWindow->height() / 2);
943 p = QPoint(desk.x() + desk.width()/2, desk.y() + desk.height()/2);
947 p = QPoint(p.x()-width()/2 - extraw,
948 p.y()-height()/2 - extrah);
951 if (p.x() + extraw + width() > desk.x() + desk.width())
952 p.setX(desk.x() + desk.width() - width() - extraw);
953 if (p.x() < desk.x())
956 if (p.y() + extrah + height() > desk.y() + desk.height())
957 p.setY(desk.y() + desk.height() - height() - extrah);
958 if (p.y() < desk.y())
965 if (QWindow *window = windowHandle())
966 window->setScreen(scrn);
973QSize QDialog::sizeHint()
const
977 if (d->orientation == Qt::Horizontal)
978 return QSize(QWidget::sizeHint().width(),
979 qMax(QWidget::sizeHint().height(),d->extension->sizeHint().height()));
981 return QSize(qMax(QWidget::sizeHint().width(), d->extension->sizeHint().width()),
982 QWidget::sizeHint().height());
984 return QWidget::sizeHint();
989QSize QDialog::minimumSizeHint()
const
993 if (d->orientation == Qt::Horizontal)
994 return QSize(QWidget::minimumSizeHint().width(),
995 qMax(QWidget::minimumSizeHint().height(), d->extension->minimumSizeHint().height()));
997 return QSize(qMax(QWidget::minimumSizeHint().width(), d->extension->minimumSizeHint().width()),
998 QWidget::minimumSizeHint().height());
1001 return QWidget::minimumSizeHint();
1035void QDialog::setSizeGripEnabled(
bool enabled)
1037#if !QT_CONFIG(sizegrip)
1041#if QT_CONFIG(sizegrip)
1042 d->sizeGripEnabled = enabled;
1043 if (enabled && d->doShowExtension)
1046 if (!enabled != !d->resizer) {
1048 d->resizer =
new QSizeGrip(
this);
1050 d->resizer->resize(d->resizer->sizeHint());
1051 if (isRightToLeft())
1052 d->resizer->move(rect().bottomLeft() -d->resizer->rect().bottomLeft());
1054 d->resizer->move(rect().bottomRight() -d->resizer->rect().bottomRight());
1055 d->resizer->raise();
1059 d->resizer =
nullptr;