diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 590c56eab8..61756a3834 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -2258,7 +2258,7 @@ bool QWindow::close() Q_D(QWindow); // Do not close non top level windows - if (parent()) + if (!isTopLevel()) return false; if (!d->platformWindow) diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp index 5c42a5da23..2534a7aeba 100644 --- a/src/widgets/kernel/qwidget.cpp +++ b/src/widgets/kernel/qwidget.cpp @@ -8466,8 +8466,10 @@ bool QWidgetPrivate::close() // Close native widgets via QWindow::close() in order to run QWindow // close code. The QWidget-specific close code in handleClose() will // in this case be called from the Close event handler in QWidgetWindow. - if (QWindow *widgetWindow = windowHandle()) - return widgetWindow->close(); + if (QWindow *widgetWindow = windowHandle()) { + if (widgetWindow->isTopLevel()) + return widgetWindow->close(); + } return handleClose(QWidgetPrivate::CloseWithEvent); } diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp index a7c3f86687..00350c3946 100644 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp +++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp @@ -243,6 +243,7 @@ private slots: void winIdChangeEvent(); void persistentWinId(); void showNativeChild(); + void closeAndShowNativeChild(); void closeAndShowWithNativeChild(); void transientParent(); void qobject_castInDestroyedSlot(); @@ -4776,6 +4777,25 @@ void tst_QWidget::showNativeChild() QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); } +void tst_QWidget::closeAndShowNativeChild() +{ + QWidget topLevel; + QWidget *nativeChild = new QWidget; + nativeChild->winId(); + nativeChild->setFixedSize(200, 200); + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(nativeChild); + topLevel.setLayout(layout); + + topLevel.show(); + QVERIFY(!nativeChild->isHidden()); + nativeChild->close(); + QVERIFY(nativeChild->isHidden()); + nativeChild->show(); + QVERIFY(!nativeChild->isHidden()); +} + void tst_QWidget::closeAndShowWithNativeChild() { bool dontCreateNativeWidgetSiblings = QApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings);