From 40b33adb7ab5d0295fa95cf66a8d3f59c8d11739 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Tue, 22 Oct 2019 13:01:38 +0200 Subject: Ensure that child windows are visible again when showing their parent When a window is closed, then it will cause the child windows to be closed as well as a result. Therefore in order to ensure that they are shown again as a result, we need to remove the WA_WState_ExplicitShowHide attribute if the widget was not already hidden before. This enables us to test for this attribute when calling showChildren(), so that if the window has a windowHandle then we can make sure that this widget is shown again. Fixes: QTBUG-73021 Change-Id: I1186242b889899dfcd38d782a67567348e2055ee Reviewed-by: Ulf Hermann (cherry picked from commit ccd3bf0871b81dfc09bb469b161f32dfb47ee53e) --- src/widgets/kernel/qwidget.cpp | 4 +++- src/widgets/kernel/qwidgetwindow.cpp | 12 ++++++++++-- tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp index f5cc6bb853..db08a1d04f 100644 --- a/src/widgets/kernel/qwidget.cpp +++ b/src/widgets/kernel/qwidget.cpp @@ -1,4 +1,4 @@ -/**************************************************************************** +/**************************************************************************** ** ** Copyright (C) 2017 The Qt Company Ltd. ** Copyright (C) 2016 Intel Corporation. @@ -8445,6 +8445,8 @@ void QWidgetPrivate::showChildren(bool spontaneous) QList childList = children; for (int i = 0; i < childList.size(); ++i) { QWidget *widget = qobject_cast(childList.at(i)); + if (widget && widget->windowHandle() && !widget->testAttribute(Qt::WA_WState_ExplicitShowHide)) + widget->setAttribute(Qt::WA_WState_Hidden, false); if (!widget || widget->isWindow() || widget->testAttribute(Qt::WA_WState_Hidden)) diff --git a/src/widgets/kernel/qwidgetwindow.cpp b/src/widgets/kernel/qwidgetwindow.cpp index fbc71cd0ea..c3f570ce4f 100644 --- a/src/widgets/kernel/qwidgetwindow.cpp +++ b/src/widgets/kernel/qwidgetwindow.cpp @@ -72,10 +72,18 @@ public: void setVisible(bool visible) override { Q_Q(QWidgetWindow); - if (QWidget *widget = q->widget()) + if (QWidget *widget = q->widget()) { + // Check if the widget was already hidden, as this indicates it was done + // explicitly and not because the parent window in this case made it hidden. + // In which case do not automatically show the widget when the parent + // window is shown. + const bool wasHidden = widget->testAttribute(Qt::WA_WState_Hidden); QWidgetPrivate::get(widget)->setVisible(visible); - else + if (!wasHidden) + widget->setAttribute(Qt::WA_WState_ExplicitShowHide, false); + } else { QWindowPrivate::setVisible(visible); + } } QWindow *eventReceiver() override { diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp index 58c61d746a..c042a92a6a 100644 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp +++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp @@ -395,6 +395,7 @@ private slots: void tabletTracking(); void closeEvent(); + void closeWithChildWindow(); private: bool ensureScreenSize(int width, int height); @@ -11067,5 +11068,27 @@ void tst_QWidget::closeEvent() QCOMPARE(widget.closeCount, 1); } +void tst_QWidget::closeWithChildWindow() +{ + QWidget widget; + auto childWidget = new QWidget(&widget); + childWidget->setAttribute(Qt::WA_NativeWindow); + childWidget->windowHandle()->create(); + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + widget.windowHandle()->close(); + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + // Check that the child window inside the window is now visible + QVERIFY(childWidget->isVisible()); + + // Now explicitly hide the childWidget + childWidget->hide(); + widget.windowHandle()->close(); + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + QVERIFY(!childWidget->isVisible()); +} + QTEST_MAIN(tst_QWidget) #include "tst_qwidget.moc" -- cgit v1.2.3