Skip to content

Commit 8882f7c

Browse files
committed
core/proxywindow: fix ProxiedWindow proxy pointer after reload
Previously was not updated after reload, causing QsWindowAttached to use the old window pointer after it had been freed.
1 parent 59298f6 commit 8882f7c

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/window/proxywindow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ void ProxyWindowBase::connectWindow() {
131131
generation->registerIncubationController(this->window->incubationController());
132132
}
133133

134+
this->window->setProxy(this);
135+
134136
// clang-format off
135137
QObject::connect(this->window, &QWindow::visibilityChanged, this, &ProxyWindowBase::visibleChanged);
136138
QObject::connect(this->window, &QWindow::xChanged, this, &ProxyWindowBase::xChanged);

src/window/proxywindow.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class ProxiedWindow: public QQuickWindow {
168168
, mProxy(proxy) {}
169169

170170
[[nodiscard]] ProxyWindowBase* proxy() const { return this->mProxy; }
171+
void setProxy(ProxyWindowBase* proxy) { this->mProxy = proxy; }
171172

172173
signals:
173174
void exposed();

src/window/test/windowattached.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <qobject.h>
44
#include <qquickitem.h>
5+
#include <qscopedpointer.h>
56
#include <qsignalspy.h>
67
#include <qtest.h>
78
#include <qtestcase.h>
@@ -36,6 +37,33 @@ void TestWindowAttachment::attachedBeforeReload() {
3637
QCOMPARE(spy.length(), 1);
3738
}
3839

40+
void TestWindowAttachment::earlyAttachReloaded() {
41+
auto window1 = QScopedPointer(new ProxyWindowBase());
42+
auto item1 = QScopedPointer(new QQuickItem());
43+
item1->setParentItem(window1->contentItem());
44+
window1->reload(nullptr);
45+
46+
auto window2 = ProxyWindowBase();
47+
auto item2 = QQuickItem();
48+
item2.setParentItem(window2.contentItem());
49+
50+
auto* attached = WindowInterface::qmlAttachedProperties(&item2);
51+
QCOMPARE_NE(attached, nullptr);
52+
QCOMPARE(attached->window(), nullptr);
53+
54+
auto spy = QSignalSpy(attached, &QsWindowAttached::windowChanged);
55+
window2.reload(window1.get());
56+
57+
QCOMPARE(attached->window(), &window2);
58+
QCOMPARE(spy.length(), 1);
59+
60+
item1.reset();
61+
window1.reset();
62+
63+
QCOMPARE(attached->window(), &window2);
64+
QCOMPARE(spy.length(), 1);
65+
}
66+
3967
void TestWindowAttachment::owningWindowChanged() {
4068
auto window1 = ProxyWindowBase();
4169
auto window2 = ProxyWindowBase();

src/window/test/windowattached.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class TestWindowAttachment: public QObject {
99
private slots:
1010
static void attachedAfterReload();
1111
static void attachedBeforeReload();
12+
static void earlyAttachReloaded();
1213
static void owningWindowChanged();
1314
static void nonItemParents();
1415
};

0 commit comments

Comments
 (0)