Hello,
I have a requirement where I need to be able to dynamically create a bunch of objects, and then be able to add them to a list of some sort so I can scroll through them. Each page can potentially be a different item with wildly different properties and functionality so coming up with a single delegate to cover all of these cases is not an option.
My thinking was to use a VisualItemModel, dynamically create whatever Items I require in Javascript code, and then parent these items I create to the VisualItemModel (with createObject) such that I can then just use this model as the source for a ListView.
I've run into a problem, however. When I call the "createObject" function on the component, I get the following error message:
QDeclarativeComponent: Created graphical object was not placed in the graphics scene.
And the "count" of the VisualItemModel does not increase, and nothing then shows up in my list. Any idea why I am getting such a message and how I can achieve what I require?
A quick chat with someone in IRC suggested that this may not be possible with VisualItemModel - but to me it seems like precisely the purpose it was designed for...
Help?
Thanks
Mark T
_______________________________________________
Qt-qml mailing list
Qt-...@qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-qml
> I have a requirement where I need to be able to dynamically create a bunch
> of objects, and then be able to add them to a list of some sort so I can
> scroll through them. Each page can potentially be a different item with
> wildly different properties and functionality so coming up with a single
> delegate to cover all of these cases is not an option.
I can't comment on your problem with VisualItemModel, but an alternative way
to approach your requirement (if I understood it correctly) would be
to use a Loader
to implement factory pattern. Something like this:
ListView {
model: dataModel
delegate: Loader {
source: if (itemType == 0) "BookDelegate.qml"
else if (itemType == 1) "SongDelegate.qml"
else "MovieDelegate.qml"
}
}
ListModel {
id: dataModel
ListElement {
itemType: 0
author: "William Shakespeare"
title: "There's something wrong in the land of Denmark"
pageCount: 654
}
ListElement {
itemType: 1
performer: "Michael Jackson"
album: "Thriller"
songTitle: "Beat it"
}
ListElement {
itemType: 2
movieTitle: "Raiders of the Lost Ark"
director: "Steven Spielberg"
}
}
I doubt using the Loader to choose the delegate gives you too much
overhead unless the expression used to choose the delegate becomes
really complex.
cheers,
Juha