Avoid creating child windows twice when already visible

When a child window is made visible without its parent having been
created yet, we defer creating and making the child visible until
the parent is created.

But if a child window is explicitly created, we create the parent
first. And creating the parent will in turn apply visibility to
all children that had their visibility deferred. Which includes
creating the child.

This results in child -> parent -> child creation recursion,
which means that when we return from creating the parent window
we already have a platform window for our child, and should
bail out.

Pick-to: 6.5
Change-Id: I11dc4864b57f031de2cca70b79cdfc057d4fbd0d
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 70d7c6a937fec04401a3c0bc6380731d9f4478e9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2023-10-18 19:02:08 +02:00 committed by Qt Cherry-pick Bot
parent 79da2a10c6
commit 0bc6150289
2 changed files with 42 additions and 0 deletions

View File

@ -526,6 +526,13 @@ void QWindowPrivate::create(bool recursive, WId nativeHandle)
if (q->parent())
q->parent()->create();
if (platformWindow) {
// Creating the parent window will end up creating any child window
// that was already visible, via setVisible. If this applies to us,
// we will already have a platform window at this point.
return;
}
// QPlatformWindow will poll geometry() during construction below. Set the
// screen here so that high-dpi scaling will use the correct scale factor.
if (q->isTopLevel()) {

View File

@ -30,6 +30,7 @@ private slots:
void create();
void setParent();
void setVisible();
void setVisibleThenCreate();
void setVisibleFalseDoesNotCreateWindow();
void eventOrderOnShow();
void paintEvent();
@ -235,6 +236,40 @@ void tst_QWindow::setVisible()
QVERIFY(QTest::qWaitForWindowExposed(&i));
}
class SurfaceCreatedWindow : public QWindow
{
Q_OBJECT
public:
using QWindow::QWindow;
bool eventFilter(QObject *o, QEvent *e) override
{
if (e->type() == QEvent::PlatformSurface) {
auto type = static_cast<QPlatformSurfaceEvent*>(e)->surfaceEventType();
if (type == QPlatformSurfaceEvent::SurfaceCreated)
++surfaceCreatedEvents;
}
return false;
}
int surfaceCreatedEvents = 0;
};
void tst_QWindow::setVisibleThenCreate()
{
QWindow parent;
parent.setObjectName("Parent");
SurfaceCreatedWindow child(&parent);
child.installEventFilter(&child);
child.setObjectName("Child");
child.setVisible(true);
child.create();
QCOMPARE(child.surfaceCreatedEvents, 1);
parent.setVisible(true);
QCOMPARE(child.surfaceCreatedEvents, 1);
QVERIFY(QTest::qWaitForWindowExposed(&child));
}
void tst_QWindow::setVisibleFalseDoesNotCreateWindow()
{
QWindow w;