PDA

View Full Version : children.count undefined



mxs
3rd December 2010, 14:28
When trying out the following test case, it prints 'Number of children: undefined' for VisualItemModel.children.count, is this the expected behaviour? I would have thought that the expected result would have been 3?

Checking the code in src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp:181, it looks the returned children object has the count method implemented.



import QtQuick 1.0
Rectangle {
width: 100
height: 100
VisualItemModel {
id: itemModel
Rectangle { height: 30; width: 80; color: "red" }
Rectangle { height: 30; width: 80; color: "green" }
Rectangle { height: 30; width: 80; color: "blue" }
}

ListView {
anchors.fill: parent
model: itemModel
focus: true
Keys.onPressed: {
console.log("Number of children: " + model.children.count);
}
}
}

wysota
3rd December 2010, 23:05
I'd say it should be model.count and not model.children.count.

mxs
6th December 2010, 10:16
The problem is that model.children is not exposing any of the functionality that I would expect. I cannot use mode.children.append etc, I put the example here of model.children.count as it was an easier code snippet to include.

Looking at the code, I would have though that I should be able to use model.children.append, but when I try and do this I get an 'undefined' error.

Matt

wysota
6th December 2010, 11:24
Where did you get this model.children from anyway?

mxs
6th December 2010, 14:03
I am no expert at javascript, but my understanding is that children is an array that is attached to all javascript objects that contains all of the child elements on the node. In the case above, it will contain the three Rectangle objects. If you dump the prototype of 'itemModel' you will see that it contains an Object called 'children'.

Checking the source code for QDeclarativeCusialItemModel as outlined above, you should see that the children property should return a QDeclarativeListProperty with the 'count' property implemented.

The main drive of this question is to try and work out how to append additional items to the list at runtime, and that would seem to be through the children.model.append function (which I can see is also implemented in the source code), but when I try and use this at runtime I get an 'undefined' error.

Regards

Matt

wysota
6th December 2010, 15:30
I am no expert at javascript, but my understanding is that children is an array that is attached to all javascript objects that contains all of the child elements on the node.
What "elements" of what "node"? If an object is a string, what are its "elements" and what is the "node"?

mxs
6th December 2010, 15:36
In the example given, the node is the model from the listview? Maybe I am not wording the question properly, but I am trying to back it up with concrete evidence of the expected behavior from the source code.

wysota
6th December 2010, 15:50
The model has a "count" member that returns the number of the items in the model but you keep trying to access some legendary "children.count" property which is supposed to return the number of children of the model object but the model object has no children - the items are not the model's children.

mxs
7th December 2010, 10:41
Ok, so I am going to ask the question in a different way in a bit, but first I would like to point out why I think there is a model.children node. Using a prototype dumper found here http://refactormycode.com/codes/226-recursively-dump-an-object, and using the following code:


port QtQuick 1.0
Rectangle {
width: 100
height: 100
VisualItemModel {
id: itemModel
Rectangle { height: 30; width: 80; color: "red" }
Rectangle { height: 30; width: 80; color: "green" }
Rectangle { height: 30; width: 80; color: "blue" }
}

ListView {
id: view
anchors.fill: parent
model: itemModel
focus: true
Keys.onPressed: {

console.log(odump(view.model));
}
}
}

I get the following output:



objectName:
itemsRemoved: function
itemsInserted: function
count: 3
modelReset: function
countChanged: function
childrenChanged: function
createdItem: function
itemsMoved: function
destroyingItem: function
children:


So as you can see, there clearly is a node called children in the prototype of model. Tracking through the source code for the declarative module as I have outlined above I can see where this node is created and what its prototype should be, however I am confused that when I try and access any member of children, its returned value is null.

So, to the reworded question. How do I alter the contents of a VisualItemModel after it has been created. My initial thought was that it should be through the children.append(item) function, but I cannot seem to get this to work.

Regards

Matt

wysota
7th December 2010, 12:22
I'd try declaring a new item (if it's possible from JavaScript at all) and setting the parent to be the visual model.