QQuickItemView/QQuickPathView: Fix creation of delegates

When the delegate is set before the model and
after the ItemView/PathView has been created

Task-number: QTBUG-38368
Change-Id: I6963abe28087699cf4e8921153dc7641bae3b220
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
This commit is contained in:
Albert Astals Cid 2014-04-15 15:27:29 +02:00 committed by The Qt Project
parent dc051f28b8
commit 5772da7e91
4 changed files with 62 additions and 0 deletions

View File

@ -364,6 +364,8 @@ void QQuickItemView::setDelegate(QQmlComponent *delegate)
if (!d->ownModel) {
d->model = new QQmlDelegateModel(qmlContext(this));
d->ownModel = true;
if (isComponentComplete())
static_cast<QQmlDelegateModel *>(d->model.data())->componentComplete();
}
if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) {
int oldCount = dataModel->count();

View File

@ -1242,6 +1242,8 @@ void QQuickPathView::setDelegate(QQmlComponent *delegate)
if (!d->ownModel) {
d->model = new QQmlDelegateModel(qmlContext(this));
d->ownModel = true;
if (isComponentComplete())
static_cast<QQmlDelegateModel *>(d->model.data())->componentComplete();
}
if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) {
int oldCount = dataModel->count();

View File

@ -102,6 +102,23 @@ Item {
}
}
ListView {
id: listViewDelegateModelAfterCreate
anchors.fill: parent
property int createdDelegates: 0
}
Component {
id: delegateModelAfterCreateComponent
Rectangle {
width: 140
height: 140
border.color: "black"
color: "red"
Component.onCompleted: listViewDelegateModelAfterCreate.createdDelegates++;
}
}
ListModel { id: emptymodel }
ListModel { id: manyitems }
ListModel { id: firstmodel; ListElement { name: "FirstModelElement0" } }
@ -249,5 +266,11 @@ Item {
asyncListViewLoaderView.currentIndex = 4;
}
}
function test_set_delegate_model_after_list_creation() {
listViewDelegateModelAfterCreate.delegate = delegateModelAfterCreateComponent;
listViewDelegateModelAfterCreate.model = 40;
verify(listViewDelegateModelAfterCreate.createdDelegates > 0);
}
}
}

View File

@ -0,0 +1,35 @@
import QtQuick 2.1
import QtTest 1.0
Item {
id: top
PathView {
id: pathViewDelegateModelAfterCreate
anchors.fill: parent
property int createdDelegates: 0
path: Path { startX: 120; startY: 100 }
}
Component {
id: delegateModelAfterCreateComponent
Rectangle {
width: 140
height: 140
border.color: "black"
color: "red"
Component.onCompleted: pathViewDelegateModelAfterCreate.createdDelegates++;
}
}
TestCase {
name: "PathView"
when: windowShown
function test_set_delegate_model_after_path_creation() {
pathViewDelegateModelAfterCreate.delegate = delegateModelAfterCreateComponent;
pathViewDelegateModelAfterCreate.model = 40;
verify(pathViewDelegateModelAfterCreate.createdDelegates > 0);
}
}
}