From 44b9aec8d900af77281f2e9209d54e946ee2fe76 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Sun, 10 Oct 2021 09:32:32 +0200 Subject: QDialog: respect WA_ShowWithoutActivating QDialog overrides setVisible to set focus on the default push button, or (if there is no such button), make the first autoDefault push button the default button. QDialog also explicitly sends a FocusIn event to the focus widget in the dialog. All this should not be done if the dialog does not become active after getting shown, which will be prevented if the WA_ShowWithoutActivating attribute is set. Add a test case. Fixes: QTBUG-8857 Pick-to: 6.2 Change-Id: If47021a4721a280ba45e95b43d0424cdacd9b4ea Reviewed-by: Richard Moe Gustavsen --- src/widgets/dialogs/qdialog.cpp | 69 ++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 32 deletions(-) (limited to 'src/widgets') diff --git a/src/widgets/dialogs/qdialog.cpp b/src/widgets/dialogs/qdialog.cpp index 9f4e95d83c..f56308da9e 100644 --- a/src/widgets/dialogs/qdialog.cpp +++ b/src/widgets/dialogs/qdialog.cpp @@ -800,42 +800,47 @@ void QDialog::setVisible(bool visible) return; QWidget::setVisible(visible); - QWidget *fw = window()->focusWidget(); - if (!fw) - fw = this; - - /* - The following block is to handle a special case, and does not - really follow propper logic in concern of autoDefault and TAB - order. However, it's here to ease usage for the users. If a - dialog has a default QPushButton, and first widget in the TAB - order also is a QPushButton, then we give focus to the main - default QPushButton. This simplifies code for the developers, - and actually catches most cases... If not, then they simply - have to use [widget*]->setFocus() themselves... - */ + + // Window activation might be prevented. We can't test isActiveWindow here, + // as the window will be activated asynchronously by the window manager. + if (!testAttribute(Qt::WA_ShowWithoutActivating)) { + QWidget *fw = window()->focusWidget(); + if (!fw) + fw = this; + + /* + The following block is to handle a special case, and does not + really follow propper logic in concern of autoDefault and TAB + order. However, it's here to ease usage for the users. If a + dialog has a default QPushButton, and first widget in the TAB + order also is a QPushButton, then we give focus to the main + default QPushButton. This simplifies code for the developers, + and actually catches most cases... If not, then they simply + have to use [widget*]->setFocus() themselves... + */ #if QT_CONFIG(pushbutton) - if (d->mainDef && fw->focusPolicy() == Qt::NoFocus) { - QWidget *first = fw; - while ((first = first->nextInFocusChain()) != fw && first->focusPolicy() == Qt::NoFocus) - ; - if (first != d->mainDef && qobject_cast(first)) - d->mainDef->setFocus(); - } - if (!d->mainDef && isWindow()) { - QWidget *w = fw; - while ((w = w->nextInFocusChain()) != fw) { - QPushButton *pb = qobject_cast(w); - if (pb && pb->autoDefault() && pb->focusPolicy() != Qt::NoFocus) { - pb->setDefault(true); - break; + if (d->mainDef && fw->focusPolicy() == Qt::NoFocus) { + QWidget *first = fw; + while ((first = first->nextInFocusChain()) != fw && first->focusPolicy() == Qt::NoFocus) + ; + if (first != d->mainDef && qobject_cast(first)) + d->mainDef->setFocus(); + } + if (!d->mainDef && isWindow()) { + QWidget *w = fw; + while ((w = w->nextInFocusChain()) != fw) { + QPushButton *pb = qobject_cast(w); + if (pb && pb->autoDefault() && pb->focusPolicy() != Qt::NoFocus) { + pb->setDefault(true); + break; + } } } - } #endif - if (fw && !fw->hasFocus()) { - QFocusEvent e(QEvent::FocusIn, Qt::TabFocusReason); - QCoreApplication::sendEvent(fw, &e); + if (fw && !fw->hasFocus()) { + QFocusEvent e(QEvent::FocusIn, Qt::TabFocusReason); + QCoreApplication::sendEvent(fw, &e); + } } #ifndef QT_NO_ACCESSIBILITY -- cgit v1.2.3