From 8a1eb34740bc5bc4db4972ab2b2a8e738dca979a Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 15 Jul 2016 16:43:17 +0200 Subject: StackView: don't crash if attached to a non-Item Change-Id: Ifa7ef9b0960494df917f9f60bf8d94cbee0f4661 Reviewed-by: Mitch Curtis --- src/quicktemplates2/qquickstackview.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src/quicktemplates2/qquickstackview.cpp') diff --git a/src/quicktemplates2/qquickstackview.cpp b/src/quicktemplates2/qquickstackview.cpp index 2aff2c38..df163089 100644 --- a/src/quicktemplates2/qquickstackview.cpp +++ b/src/quicktemplates2/qquickstackview.cpp @@ -289,12 +289,7 @@ QQuickStackView::~QQuickStackView() QQuickStackAttached *QQuickStackView::qmlAttachedProperties(QObject *object) { - QQuickItem *item = qobject_cast(object); - if (!item) { - qmlInfo(object) << "StackView must be attached to an Item"; - return nullptr; - } - return new QQuickStackAttached(item); + return new QQuickStackAttached(object); } /*! @@ -993,19 +988,25 @@ void QQuickStackAttachedPrivate::itemParentChanged(QQuickItem *item, QQuickItem emit q->statusChanged(); } -QQuickStackAttached::QQuickStackAttached(QQuickItem *parent) : +QQuickStackAttached::QQuickStackAttached(QObject *parent) : QObject(*(new QQuickStackAttachedPrivate), parent) { Q_D(QQuickStackAttached); - QQuickItemPrivate::get(parent)->addItemChangeListener(d, QQuickItemPrivate::Parent); - d->itemParentChanged(parent, parent->parentItem()); + QQuickItem *item = qobject_cast(parent); + if (item) { + QQuickItemPrivate::get(item)->addItemChangeListener(d, QQuickItemPrivate::Parent); + d->itemParentChanged(item, item->parentItem()); + } else if (parent) { + qmlInfo(parent) << "StackView must be attached to an Item"; + } } QQuickStackAttached::~QQuickStackAttached() { Q_D(QQuickStackAttached); - QQuickItem *parentItem = static_cast(parent()); - QQuickItemPrivate::get(parentItem)->removeItemChangeListener(d, QQuickItemPrivate::Parent); + QQuickItem *parentItem = qobject_cast(parent()); + if (parentItem) + QQuickItemPrivate::get(parentItem)->removeItemChangeListener(d, QQuickItemPrivate::Parent); } /*! -- cgit v1.2.3