aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2023-01-27 12:05:09 +0100
committerUlf Hermann <ulf.hermann@qt.io>2023-01-31 11:52:53 +0100
commitb04776e82eaa0fdda1ede1b5635844eddc6c3501 (patch)
tree4b1b39e750f7b18193113399193d5eb16c2a8478
parentd31e86f8066bf5d47c480105d579d53ae223aabf (diff)
Item views: Don't create unnecessary QML contexts
Bound components can only be instantiated in the context they're declared in. Adding a context in between before instantiating a component makes it impossible to bind the component. We want to use bound components so that we can safely use IDs of other objects from the same context inside the objects created from the component. See also commit fc683799fee933757abdd3953048f136e750690b. Pick-to: 6.5 Fixes: QTBUG-110697 Change-Id: I8ac9afe13a3d88a4c8e432f5cd701abee8a7f6ac Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/quick/items/qquickitemview.cpp13
-rw-r--r--tests/auto/quick/qquicklistview2/data/highlightWithBound.qml10
-rw-r--r--tests/auto/quick/qquicklistview2/tst_qquicklistview2.cpp15
3 files changed, 30 insertions, 8 deletions
diff --git a/src/quick/items/qquickitemview.cpp b/src/quick/items/qquickitemview.cpp
index 1f3ec79190..a149f8d098 100644
--- a/src/quick/items/qquickitemview.cpp
+++ b/src/quick/items/qquickitemview.cpp
@@ -2447,17 +2447,14 @@ QQuickItem *QQuickItemViewPrivate::createComponentItem(QQmlComponent *component,
QQuickItem *item = nullptr;
if (component) {
- QQmlContext *creationContext = component->creationContext();
- QQmlContext *context = new QQmlContext(
- creationContext ? creationContext : qmlContext(q));
- QObject *nobj = component->beginCreate(context);
- if (nobj) {
- QQml_setParent_noEvent(context, nobj);
+ QQmlContext *context = component->creationContext();
+ if (!context)
+ context = qmlContext(q);
+
+ if (QObject *nobj = component->beginCreate(context)) {
item = qobject_cast<QQuickItem *>(nobj);
if (!item)
delete nobj;
- } else {
- delete context;
}
} else if (createDefault) {
item = new QQuickItem;
diff --git a/tests/auto/quick/qquicklistview2/data/highlightWithBound.qml b/tests/auto/quick/qquicklistview2/data/highlightWithBound.qml
new file mode 100644
index 0000000000..6cedd3e7d3
--- /dev/null
+++ b/tests/auto/quick/qquicklistview2/data/highlightWithBound.qml
@@ -0,0 +1,10 @@
+pragma ComponentBehavior: Bound
+import QtQuick
+
+ListView {
+ model: 3
+ delegate: Item {}
+ highlight: Item {
+ objectName: "highlight"
+ }
+}
diff --git a/tests/auto/quick/qquicklistview2/tst_qquicklistview2.cpp b/tests/auto/quick/qquicklistview2/tst_qquicklistview2.cpp
index a86504599c..117ec0c94d 100644
--- a/tests/auto/quick/qquicklistview2/tst_qquicklistview2.cpp
+++ b/tests/auto/quick/qquicklistview2/tst_qquicklistview2.cpp
@@ -51,6 +51,7 @@ private slots:
void isCurrentItem_NoRegressionWithDelegateModelGroups();
void pullbackSparseList();
+ void highlightWithBound();
private:
void flickWithTouch(QQuickWindow *window, const QPoint &from, const QPoint &to);
@@ -931,6 +932,20 @@ void tst_QQuickListView2::pullbackSparseList() // QTBUG_104679
QVERIFY(QTest::qWaitForWindowExposed(window.data()));
}
+void tst_QQuickListView2::highlightWithBound()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, testFileUrl("highlightWithBound.qml"));
+ QVERIFY2(c.isReady(), qPrintable(c.errorString()));
+ QScopedPointer<QObject> o(c.create());
+ QVERIFY(!o.isNull());
+ QQuickListView *listView = qobject_cast<QQuickListView *>(o.data());
+ QVERIFY(listView);
+ QQuickItem *highlight = listView->highlightItem();
+ QVERIFY(highlight);
+ QCOMPARE(highlight->objectName(), QStringLiteral("highlight"));
+}
+
QTEST_MAIN(tst_QQuickListView2)
#include "tst_qquicklistview2.moc"