Prevent errors when removing items from Repeater that reference parent.

Use the same ordering as item views and release before unparenting.

Change-Id: I0346342cfcaf9385d8385769795dd5ba35fc43aa
Task-number: QTBUG-46828
Reviewed-by: Liang Qi <liang.qi@theqtcompany.com>
Reviewed-by: Robin Burchell <robin.burchell@viroteck.net>
This commit is contained in:
Michael Brasser 2015-06-23 13:13:01 -05:00
parent f869d0e550
commit 9d0e702b1f
3 changed files with 40 additions and 2 deletions

View File

@ -361,8 +361,8 @@ void QQuickRepeater::clear()
if (QQuickItem *item = d->deletables.at(i)) {
if (complete)
emit itemRemoved(i, item);
item->setParentItem(0);
d->model->release(item);
item->setParentItem(0);
}
}
}
@ -466,8 +466,8 @@ void QQuickRepeater::modelUpdated(const QQmlChangeSet &changeSet, bool reset)
d->deletables.remove(index);
emit itemRemoved(index, item);
if (item) {
item->setParentItem(0);
d->model->release(item);
item->setParentItem(0);
}
--d->itemCount;
}

View File

@ -0,0 +1,17 @@
import QtQuick 2.0
Row {
spacing: 2
height: 100
Repeater {
id: repeater
objectName: "repeater"
model: 10
Rectangle {
color: "green"
width: 10; height: 50
anchors.bottom: parent.bottom
}
}
}

View File

@ -68,6 +68,7 @@ private slots:
void resetModel();
void modelChanged();
void modelReset();
void modelCleared();
void properties();
void asynchronous();
void initParent();
@ -634,6 +635,26 @@ void tst_QQuickRepeater::modelReset()
QCOMPARE(addedSpy.count(), 0);
}
// QTBUG-46828
void tst_QQuickRepeater::modelCleared()
{
QQmlEngine engine;
QQmlComponent component(&engine, testFileUrl("modelCleared.qml"));
QQuickItem *rootObject = qobject_cast<QQuickItem*>(component.create());
QVERIFY(rootObject);
QQuickRepeater *repeater = findItem<QQuickRepeater>(rootObject, "repeater");
QVERIFY(repeater);
// verify no error messages when the model is cleared and the items are destroyed
QQmlTestMessageHandler messageHandler;
repeater->setModel(0);
QVERIFY2(messageHandler.messages().isEmpty(), qPrintable(messageHandler.messageString()));
delete rootObject;
}
void tst_QQuickRepeater::properties()
{
QQmlEngine engine;