[Qt-qml] Dynamically adding items to a VisualItemModel

663 views
Skip to first unread message

Mark Tucker

unread,
Jan 9, 2012, 11:51:18 AM1/9/12
to qt-...@qt.nokia.com

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

Artem Marchenko

unread,
Jan 10, 2012, 9:16:27 AM1/10/12
to Mark Tucker, qt-...@qt.nokia.com
Hi Mark

I had a similar situation in the past and I solved it in pure QML/JavaScript using ListModel and its append/insert/set methods. Worked fine, but certainly that puts a limit on the data structures used as items (you can store only simple data types)

Best regards,
Artem.

_______________________________________________
Qt-qml mailing list
Qt-...@qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-qml

Juha Turunen

unread,
Jan 10, 2012, 9:56:01 AM1/10/12
to qt-...@qt.nokia.com
Hi Mark,

> 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

Reply all
Reply to author
Forward
0 new messages