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 auto type = themeDialogType(dialog);
91 QGuiApplicationPrivate::platformTheme()->createPlatformDialogHelper(*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 auto type = themeDialogType(dialog);
111 return QGuiApplicationPrivate::platformTheme()->usePlatformNativeDialog(*type);
116
117
118
119
120void QDialogPrivate::close(
int resultCode)
124 q->setResult(resultCode);
126 if (!data.is_closing) {
130 struct CloseEventEater : QObject
132 using QObject::QObject;
134 bool eventFilter(QObject *o, QEvent *e) override
136 if (e->type() == QEvent::Close)
138 return QObject::eventFilter(o, e);
141 q->installEventFilter(&closeEventEater);
142 QWidgetPrivate::close();
151 resetModalitySetByOpen();
154void QDialogPrivate::recreate()
159 QPointer<QEventLoop> existingEventLoop = std::exchange(eventLoop,
nullptr);
160 QWidgetPrivate::recreate();
161 eventLoop = existingEventLoop;
164QWindow *QDialogPrivate::transientParentWindow()
const
167 if (
const QWidget *parent = q->nativeParentWidget())
168 return parent->windowHandle();
169 else if (q->windowHandle())
170 return q->windowHandle()->transientParent();
174bool QDialogPrivate::setNativeDialogVisible(
bool visible)
176 if (QPlatformDialogHelper *helper = platformHelper()) {
179 helperPrepareShow(helper);
180 nativeDialogInUse = helper->show(q->windowFlags(), q->windowModality(), transientParentWindow());
181 }
else if (nativeDialogInUse) {
185 return nativeDialogInUse;
188QVariant QDialogPrivate::styleHint(QPlatformDialogHelper::StyleHint hint)
const
190 if (
const QPlatformDialogHelper *helper = platformHelper())
191 return helper->styleHint(hint);
192 return QPlatformDialogHelper::defaultStyleHint(hint);
196
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
356
357
358
359
360
361
364
365
366
367
368
369
373
374
375
376
377
378
379
380
381
382
383
384
386QDialog::QDialog(QWidget *parent, Qt::WindowFlags f)
387 : QWidget(*
new QDialogPrivate, parent,
388 f | ((f & Qt::WindowType_Mask) == 0 ? Qt::Dialog : Qt::WindowType(0)))
393
394
395
396QDialog::QDialog(QDialogPrivate &dd, QWidget *parent, Qt::WindowFlags f)
397 : QWidget(dd, parent, f | ((f & Qt::WindowType_Mask) == 0 ? Qt::Dialog : Qt::WindowType(0)))
402
403
417
418
419
420
421
422
423#if QT_CONFIG(pushbutton)
424void QDialogPrivate::setDefault(QPushButton *pushButton)
427 bool hasMain =
false;
428 const auto pbList = q->findChildren<QPushButton *>();
429 for (QPushButton *pb : pbList) {
430 if (pb->window() == q) {
433 if (pb != pushButton)
434 pb->setDefault(
false);
437 if (!pushButton && hasMain)
438 mainDef->setDefault(
true);
440 mainDef = pushButton;
444
445
446
447
448void QDialogPrivate::setMainDefault(QPushButton *pushButton)
451 setDefault(pushButton);
455
456
457
458
459void QDialogPrivate::hideDefault()
462 const auto pbList = q->findChildren<QPushButton *>();
463 for (QPushButton *pb : pbList)
464 pb->setDefault(
false);
468void QDialogPrivate::resetModalitySetByOpen()
471 if (resetModalityTo != -1 && !q->testAttribute(Qt::WA_SetWindowModality)) {
473 q->setWindowModality(Qt::WindowModality(resetModalityTo));
474 q->setAttribute(Qt::WA_SetWindowModality, wasModalitySet);
476 Q_ASSERT(resetModalityTo != Qt::WindowModal);
477 q->setParent(q->parentWidget(), Qt::Dialog);
480 resetModalityTo = -1;
484
485
486
487
488
489
490
491
492
493int QDialog::result()
const
500
501
502
503
504
505
506
507void QDialog::setResult(
int r)
514
515
516
517
518
523 Qt::WindowModality modality = windowModality();
524 if (modality != Qt::WindowModal) {
525 d->resetModalityTo = modality;
526 d->wasModalitySet = testAttribute(Qt::WA_SetWindowModality);
527 setWindowModality(Qt::WindowModal);
528 setAttribute(Qt::WA_SetWindowModality,
false);
530 setParent(parentWidget(), Qt::Sheet);
563 if (Q_UNLIKELY(d->eventLoop)) {
564 qWarning(
"QDialog::exec: Recursive call detected");
568 bool deleteOnClose = testAttribute(Qt::WA_DeleteOnClose);
569 setAttribute(Qt::WA_DeleteOnClose,
false);
571 d->resetModalitySetByOpen();
573 bool wasShowModal = testAttribute(Qt::WA_ShowModal);
574 setAttribute(Qt::WA_ShowModal,
true);
579 QPointer<QDialog> guard =
this;
580 if (d->nativeDialogInUse) {
581 d->platformHelper()->exec();
583 QEventLoop eventLoop;
584 d->eventLoop = &eventLoop;
585 (
void) eventLoop.exec(QEventLoop::DialogExec);
588 return QDialog::Rejected;
589 d->eventLoop =
nullptr;
591 setAttribute(Qt::WA_ShowModal, wasShowModal);
594 if (d->nativeDialogInUse)
595 d->helperDone(
static_cast<QDialog::DialogCode>(res), d->platformHelper());
672void QDialog::contextMenuEvent(QContextMenuEvent *e)
674#if !QT_CONFIG(whatsthis) || !QT_CONFIG(menu)
677 QWidget *w = childAt(e->pos());
679 w = rect().contains(e->pos()) ?
this :
nullptr;
683 while (w && w->whatsThis().size() == 0 && !w->testAttribute(Qt::WA_CustomWhatsThis))
684 w = w->isWindow() ?
nullptr : w->parentWidget();
686 QPointer<QMenu> p =
new QMenu(
this);
687 QAction *wt = p.data()->addAction(tr(
"What's This?"));
688 if (p.data()->exec(e->globalPos()) == wt) {
689 QHelpEvent e(QEvent::WhatsThis, w->rect().center(),
690 w->mapToGlobal(w->rect().center()));
691 QCoreApplication::sendEvent(w, &e);
700void QDialog::keyPressEvent(QKeyEvent *e)
702#ifndef QT_NO_SHORTCUT
706 if (e->matches(QKeySequence::Cancel)) {
710 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {
712#if QT_CONFIG(pushbutton)
714 case Qt::Key_Return: {
715 const auto pbList = findChildren<QPushButton *>();
716 for (QPushButton *pb : pbList) {
717 if (pb->isDefault() && pb->isVisible()) {
764void QDialogPrivate::setVisible(
bool visible)
767 if (!q->testAttribute(Qt::WA_DontShowOnScreen) && canBeNativeDialog() && setNativeDialogVisible(visible))
773 const bool dontBlockWindows = q->testAttribute(Qt::WA_DontShowOnScreen)
774 && styleHint(QPlatformDialogHelper::DialogIsQtWindow).toBool();
775 Qt::WindowModality oldModality;
778 if (dontBlockWindows) {
779 oldModality = q->windowModality();
780 wasModalitySet = q->testAttribute(Qt::WA_SetWindowModality);
781 q->setWindowModality(Qt::NonModal);
785 QWidgetPrivate::setVisible(visible);
789 if (!q->testAttribute(Qt::WA_ShowWithoutActivating)) {
790 QWidget *fw = q->window()->focusWidget();
795
796
797
798
799
800
801
802
803
804#if QT_CONFIG(pushbutton)
805 if (mainDef && fw->focusPolicy() == Qt::NoFocus) {
807 while ((first = first->nextInFocusChain()) != fw && first->focusPolicy() == Qt::NoFocus)
809 if (first != mainDef && qobject_cast<QPushButton*>(first))
812 if (!mainDef && q->isWindow()) {
814 while ((w = w->nextInFocusChain()) != fw) {
815 QPushButton *pb = qobject_cast<QPushButton *>(w);
816 if (pb && pb->autoDefault() && pb->focusPolicy() != Qt::NoFocus) {
817 pb->setDefault(
true);
823 if (fw && !fw->hasFocus()) {
824 QFocusEvent e(QEvent::FocusIn, Qt::TabFocusReason);
825 QCoreApplication::sendEvent(fw, &e);
829#if QT_CONFIG(accessibility)
830 QAccessibleEvent event(q, QAccessible::DialogStart);
831 QAccessible::updateAccessibility(&event);
836#if QT_CONFIG(accessibility)
837 if (q->isVisible()) {
838 QAccessibleEvent event(q, QAccessible::DialogEnd);
839 QAccessible::updateAccessibility(&event);
844 QWidgetPrivate::setVisible(visible);
849 if (dontBlockWindows) {
850 q->setWindowModality(oldModality);
851 q->setAttribute(Qt::WA_SetWindowModality, wasModalitySet);
854#if QT_CONFIG(pushbutton)
855 const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
856 if (mainDef && q->isActiveWindow()
857 && theme->themeHint(QPlatformTheme::DialogSnapToDefaultButton).toBool())
858 QCursor::setPos(mainDef->mapToGlobal(mainDef->rect().center()));
863void QDialog::showEvent(QShowEvent *event)
865 if (!event->spontaneous() && !testAttribute(Qt::WA_Moved)) {
866 Qt::WindowStates state = windowState();
867 adjustPosition(parentWidget());
868 setAttribute(Qt::WA_Moved,
false);
869 if (state != windowState())
870 setWindowState(state);
875void QDialog::adjustPosition(QWidget* w)
879 if (
const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
880 if (theme->themeHint(QPlatformTheme::WindowAutoPlacement).toBool())
883 int extraw = 0, extrah = 0;
884 const QWindow *parentWindow =
nullptr;
888 parentWindow = d->transientParentWindow();
891 QScreen *scrn =
nullptr;
894 else if (parentWindow)
895 scrn = parentWindow->screen();
896 else if (QGuiApplication::primaryScreen()->virtualSiblings().size() > 1)
897 scrn = QGuiApplication::screenAt(QCursor::pos());
901 desk = scrn->availableGeometry();
903 const QWidgetList tlwList = QApplication::topLevelWidgets();
904 for (
const QWidget *current : tlwList) {
905 if (current->isVisible()) {
906 int framew = current->geometry().x() - current->x();
907 int frameh = current->geometry().y() - current->y();
909 extraw = qMax(extraw, framew);
910 extrah = qMax(extrah, frameh);
911 if (extraw != 0 && extrah != 0)
918 if (extraw == 0 || extrah == 0 || extraw >= 10 || extrah >= 40) {
927 if (w->windowHandle() && qvariant_cast<WId>(w->windowHandle()->property(
"_q_embedded_native_parent_handle")))
930 pp = w->mapToGlobal(QPoint(0,0));
931 p = QPoint(pp.x() + w->width()/2,
932 pp.y() + w->height()/ 2);
933 }
else if (parentWindow) {
936 QPoint pp = parentWindow->mapToGlobal(QPoint(0, 0));
937 p = QPoint(pp.x() + parentWindow->width() / 2, pp.y() + parentWindow->height() / 2);
940 p = QPoint(desk.x() + desk.width()/2, desk.y() + desk.height()/2);
944 p = QPoint(p.x()-width()/2 - extraw,
945 p.y()-height()/2 - extrah);
948 if (p.x() + extraw + width() > desk.x() + desk.width())
949 p.setX(desk.x() + desk.width() - width() - extraw);
950 if (p.x() < desk.x())
953 if (p.y() + extrah + height() > desk.y() + desk.height())
954 p.setY(desk.y() + desk.height() - height() - extrah);
955 if (p.y() < desk.y())
962QSize QDialog::sizeHint()
const
966 if (d->orientation == Qt::Horizontal)
967 return QSize(QWidget::sizeHint().width(),
968 qMax(QWidget::sizeHint().height(),d->extension->sizeHint().height()));
970 return QSize(qMax(QWidget::sizeHint().width(), d->extension->sizeHint().width()),
971 QWidget::sizeHint().height());
973 return QWidget::sizeHint();
978QSize QDialog::minimumSizeHint()
const
982 if (d->orientation == Qt::Horizontal)
983 return QSize(QWidget::minimumSizeHint().width(),
984 qMax(QWidget::minimumSizeHint().height(), d->extension->minimumSizeHint().height()));
986 return QSize(qMax(QWidget::minimumSizeHint().width(), d->extension->minimumSizeHint().width()),
987 QWidget::minimumSizeHint().height());
990 return QWidget::minimumSizeHint();
1024void QDialog::setSizeGripEnabled(
bool enabled)
1026#if !QT_CONFIG(sizegrip)
1030#if QT_CONFIG(sizegrip)
1031 d->sizeGripEnabled = enabled;
1032 if (enabled && d->doShowExtension)
1035 if (!enabled != !d->resizer) {
1037 d->resizer =
new QSizeGrip(
this);
1039 d->resizer->resize(d->resizer->sizeHint());
1040 if (isRightToLeft())
1041 d->resizer->move(rect().bottomLeft() -d->resizer->rect().bottomLeft());
1043 d->resizer->move(rect().bottomRight() -d->resizer->rect().bottomRight());
1044 d->resizer->raise();
1048 d->resizer =
nullptr;