diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 47277abea30..6092b08ea15 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -532,7 +532,9 @@ void QWindow::setVisible(bool visible) // can defer creation until the parent is created or we're re-parented. if (parent() && !parent()->handle()) return; - else + + // We only need to create the window if it's being shown + if (visible) create(); } @@ -574,7 +576,8 @@ void QWindow::setVisible(bool visible) d->applyCursor(); #endif - d->platformWindow->setVisible(visible); + if (d->platformWindow) + d->platformWindow->setVisible(visible); if (!visible) { QHideEvent hideEvent; diff --git a/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp b/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp index 4fc18be3d25..36ec28de8dc 100644 --- a/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp +++ b/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp @@ -56,6 +56,7 @@ private slots: void create(); void setParent(); void setVisible(); + void setVisibleFalseDoesNotCreateWindow(); void eventOrderOnShow(); void resizeEventAfterResize(); void exposeEventOnShrink_QTBUG54040(); @@ -234,6 +235,16 @@ void tst_QWindow::setVisible() QVERIFY(QTest::qWaitForWindowExposed(&i)); } +void tst_QWindow::setVisibleFalseDoesNotCreateWindow() +{ + QWindow w; + QVERIFY(!w.handle()); + w.setVisible(false); + QVERIFY2(!w.handle(), "Hiding a non-created window doesn't create it"); + w.setVisible(true); + QVERIFY2(w.handle(), "Showing a non-created window creates it"); +} + void tst_QWindow::mapGlobal() { QWindow a;