From d25fc4c63fa4512aa1cfc5ce3877e4d8632b3fec Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 4 Jul 2012 14:11:50 +1000 Subject: Qml ListView highlight component cannot access ListView scope Use the component creationContext to create the highlight item. Also do this for the header and footer. Task-number: QTBUG-26043 Change-Id: Ib4b947d022cb1513f728ac66455bfd2fe2703744 Reviewed-by: Bea Lam --- .../graphicsitems/qdeclarativegridview.cpp | 56 ++++++++++------------ .../graphicsitems/qdeclarativelistview.cpp | 56 ++++++++++------------ 2 files changed, 48 insertions(+), 64 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp index 868b6db..daf55a1 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -165,6 +165,7 @@ public: void clear(); FxGridItem *createItem(int modelIndex); void releaseItem(FxGridItem *item); + QDeclarativeItem *createComponentItem(QDeclarativeComponent *component); void refill(qreal from, qreal to, bool doBuffer=false); void updateGrid(); @@ -557,6 +558,26 @@ void QDeclarativeGridViewPrivate::releaseItem(FxGridItem *item) delete item; } +QDeclarativeItem *QDeclarativeGridViewPrivate::createComponentItem(QDeclarativeComponent *component) +{ + Q_Q(QDeclarativeGridView); + QDeclarativeItem *item = 0; + QDeclarativeContext *creationContext = component->creationContext(); + QDeclarativeContext *context = new QDeclarativeContext( + creationContext ? creationContext : qmlContext(q)); + QObject *nobj = component->create(context); + if (nobj) { + QDeclarative_setParent_noEvent(context, nobj); + item = qobject_cast(nobj); + if (!item) + delete nobj; + } else { + delete context; + } + + return item; +} + void QDeclarativeGridViewPrivate::refill(qreal from, qreal to, bool doBuffer) { Q_Q(QDeclarativeGridView); @@ -838,16 +859,7 @@ void QDeclarativeGridViewPrivate::createHighlight() if (currentItem) { QDeclarativeItem *item = 0; if (highlightComponent) { - QDeclarativeContext *highlightContext = new QDeclarativeContext(qmlContext(q)); - QObject *nobj = highlightComponent->create(highlightContext); - if (nobj) { - QDeclarative_setParent_noEvent(highlightContext, nobj); - item = qobject_cast(nobj); - if (!item) - delete nobj; - } else { - delete highlightContext; - } + item = createComponentItem(highlightComponent); } else { item = new QDeclarativeItem; QDeclarative_setParent_noEvent(item, q->contentItem()); @@ -935,17 +947,7 @@ void QDeclarativeGridViewPrivate::updateFooter() { Q_Q(QDeclarativeGridView); if (!footer && footerComponent) { - QDeclarativeItem *item = 0; - QDeclarativeContext *context = new QDeclarativeContext(qmlContext(q)); - QObject *nobj = footerComponent->create(context); - if (nobj) { - QDeclarative_setParent_noEvent(context, nobj); - item = qobject_cast(nobj); - if (!item) - delete nobj; - } else { - delete context; - } + QDeclarativeItem *item = createComponentItem(footerComponent); if (item) { QDeclarative_setParent_noEvent(item, q->contentItem()); item->setParentItem(q->contentItem()); @@ -988,17 +990,7 @@ void QDeclarativeGridViewPrivate::updateHeader() { Q_Q(QDeclarativeGridView); if (!header && headerComponent) { - QDeclarativeItem *item = 0; - QDeclarativeContext *context = new QDeclarativeContext(qmlContext(q)); - QObject *nobj = headerComponent->create(context); - if (nobj) { - QDeclarative_setParent_noEvent(context, nobj); - item = qobject_cast(nobj); - if (!item) - delete nobj; - } else { - delete context; - } + QDeclarativeItem *item = createComponentItem(headerComponent); if (item) { QDeclarative_setParent_noEvent(item, q->contentItem()); item->setParentItem(q->contentItem()); diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp index ac91c66..be327a8 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview.cpp +++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp @@ -217,6 +217,7 @@ public: void clear(); FxListItem *createItem(int modelIndex); void releaseItem(FxListItem *item); + QDeclarativeItem *createComponentItem(QDeclarativeComponent *component); FxListItem *visibleItem(int modelIndex) const { if (modelIndex >= visibleIndex && modelIndex < visibleIndex + visibleItems.count()) { @@ -677,6 +678,26 @@ FxListItem *QDeclarativeListViewPrivate::createItem(int modelIndex) return listItem; } +QDeclarativeItem *QDeclarativeListViewPrivate::createComponentItem(QDeclarativeComponent *component) +{ + Q_Q(QDeclarativeListView); + QDeclarativeItem *item = 0; + QDeclarativeContext *creationContext = component->creationContext(); + QDeclarativeContext *context = new QDeclarativeContext( + creationContext ? creationContext : qmlContext(q)); + QObject *nobj = component->create(context); + if (nobj) { + QDeclarative_setParent_noEvent(context, nobj); + item = qobject_cast(nobj); + if (!item) + delete nobj; + } else { + delete context; + } + + return item; +} + void QDeclarativeListViewPrivate::releaseItem(FxListItem *item) { Q_Q(QDeclarativeListView); @@ -946,16 +967,7 @@ void QDeclarativeListViewPrivate::createHighlight() if (currentItem) { QDeclarativeItem *item = 0; if (highlightComponent) { - QDeclarativeContext *highlightContext = new QDeclarativeContext(qmlContext(q)); - QObject *nobj = highlightComponent->create(highlightContext); - if (nobj) { - QDeclarative_setParent_noEvent(highlightContext, nobj); - item = qobject_cast(nobj); - if (!item) - delete nobj; - } else { - delete highlightContext; - } + item = createComponentItem(highlightComponent); } else { item = new QDeclarativeItem; } @@ -1198,17 +1210,7 @@ void QDeclarativeListViewPrivate::updateFooter() { Q_Q(QDeclarativeListView); if (!footer && footerComponent) { - QDeclarativeItem *item = 0; - QDeclarativeContext *context = new QDeclarativeContext(qmlContext(q)); - QObject *nobj = footerComponent->create(context); - if (nobj) { - QDeclarative_setParent_noEvent(context, nobj); - item = qobject_cast(nobj); - if (!item) - delete nobj; - } else { - delete context; - } + QDeclarativeItem *item = createComponentItem(footerComponent); if (item) { QDeclarative_setParent_noEvent(item, q->contentItem()); item->setParentItem(q->contentItem()); @@ -1238,17 +1240,7 @@ void QDeclarativeListViewPrivate::updateHeader() { Q_Q(QDeclarativeListView); if (!header && headerComponent) { - QDeclarativeItem *item = 0; - QDeclarativeContext *context = new QDeclarativeContext(qmlContext(q)); - QObject *nobj = headerComponent->create(context); - if (nobj) { - QDeclarative_setParent_noEvent(context, nobj); - item = qobject_cast(nobj); - if (!item) - delete nobj; - } else { - delete context; - } + QDeclarativeItem *item = createComponentItem(headerComponent); if (item) { QDeclarative_setParent_noEvent(item, q->contentItem()); item->setParentItem(q->contentItem()); -- cgit v0.12