diff options
author | Cathy Park <cathy.park@lge.com> | 2021-08-19 15:40:27 +0900 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-08-26 09:33:04 +0000 |
commit | b1f7d1380cef500dba25586c0b6e95550e986343 (patch) | |
tree | a71bbc2c70061e2480f0810071047dd1c1328031 /tests | |
parent | 5ae4f2a1dceda33e0d732fcca5d5f0ed15758eb5 (diff) |
qqmllistmodel: Fix QObjects setting indestructible
It makes a QObject indestructable only if its ownership is determined.
This fixes an issue where a QObject created by createObject() in QML
becomes indestructable once it is appended to a ListModel.
Fixes: QTBUG-95895
Change-Id: I30647298977d7901dab938626e8f94b9910c21e3
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Reviewed-by: Jaeyoon Jung <jaeyoon.jung@lge.com>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
(cherry picked from commit 69e07c55ad9b49b7643ffddfedc9a558983272ad)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmllistmodel/data/destroyObject.qml | 23 | ||||
-rw-r--r-- | tests/auto/qml/qqmllistmodel/data/dummyItem.qml | 5 | ||||
-rw-r--r-- | tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp | 26 |
3 files changed, 54 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmllistmodel/data/destroyObject.qml b/tests/auto/qml/qqmllistmodel/data/destroyObject.qml new file mode 100644 index 0000000000..e6b3f33bb9 --- /dev/null +++ b/tests/auto/qml/qqmllistmodel/data/destroyObject.qml @@ -0,0 +1,23 @@ +import QtQml + +QtObject { + id: root + property ListModel projects: ListModel {} + property var object + + Component.onCompleted: { + var comp= Qt.createComponent("dummyItem.qml"); + object = comp.createObject(root, {}); + projects.append({"obj": object}); + } + + function destroy() { + try { + object.destroy(); + } catch(e) { + console.warn(e); + return false; + } + return true; + } +} diff --git a/tests/auto/qml/qqmllistmodel/data/dummyItem.qml b/tests/auto/qml/qqmllistmodel/data/dummyItem.qml new file mode 100644 index 0000000000..c9939efcdf --- /dev/null +++ b/tests/auto/qml/qqmllistmodel/data/dummyItem.qml @@ -0,0 +1,5 @@ +import QtQml + +QtObject { + property var random: null +} diff --git a/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp b/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp index a9b6ec03a4..7f94180274 100644 --- a/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp +++ b/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp @@ -136,6 +136,7 @@ private slots: void destroyObject(); void emptyStringNotUndefined(); void listElementWithTemplateString(); + void destroyComponentObject(); }; bool tst_qqmllistmodel::compareVariantList(const QVariantList &testList, QVariant object) @@ -1856,6 +1857,31 @@ void tst_qqmllistmodel::listElementWithTemplateString() QVERIFY(!root.isNull()); } +//QTBUG-95895 +void tst_qqmllistmodel::destroyComponentObject() +{ + QQmlEngine eng; + QQmlComponent component(&eng, testFileUrl("destroyObject.qml")); + QVERIFY(!component.isError()); + QScopedPointer<QObject> obj(component.create()); + QVERIFY(!obj.isNull()); + QQmlListModel *list = qvariant_cast<QQmlListModel *>(obj->property("projects")); + QVERIFY(list != nullptr); + QCOMPARE(list->count(), 1); + QPointer<QObject> created(qvariant_cast<QObject *>(obj->property("object"))); + QVERIFY(!created.isNull()); + QCOMPARE(list->get(0).property("obj").toQObject(), created.data()); + QVariant retVal; + QMetaObject::invokeMethod(obj.data(), + "destroy", + Qt::DirectConnection, + Q_RETURN_ARG(QVariant, retVal)); + QVERIFY(retVal.toBool()); + QTRY_VERIFY(created.isNull()); + QTRY_VERIFY(list->get(0).property("obj").isUndefined()); + QCOMPARE(list->count(), 1); +} + QTEST_MAIN(tst_qqmllistmodel) #include "tst_qqmllistmodel.moc" |