Results 1 to 10 of 10

Thread: children.count undefined

  1. #1
    Join Date
    Dec 2010
    Posts
    6

    Default children.count undefined

    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.

    Qt Code:
    1. import QtQuick 1.0
    2. Rectangle {
    3. width: 100
    4. height: 100
    5. VisualItemModel {
    6. id: itemModel
    7. Rectangle { height: 30; width: 80; color: "red" }
    8. Rectangle { height: 30; width: 80; color: "green" }
    9. Rectangle { height: 30; width: 80; color: "blue" }
    10. }
    11.  
    12. ListView {
    13. anchors.fill: parent
    14. model: itemModel
    15. focus: true
    16. Keys.onPressed: {
    17. console.log("Number of children: " + model.children.count);
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: children.count undefined

    I'd say it should be model.count and not model.children.count.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2010
    Posts
    6

    Default Re: children.count undefined

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: children.count undefined

    Where did you get this model.children from anyway?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2010
    Posts
    6

    Default Re: children.count undefined

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: children.count undefined

    Quote Originally Posted by mxs View Post
    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"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Dec 2010
    Posts
    6

    Default Re: children.count undefined

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: children.count undefined

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Dec 2010
    Posts
    6

    Default Re: children.count undefined

    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-...dump-an-object, and using the following code:
    Qt Code:
    1. port QtQuick 1.0
    2. Rectangle {
    3. width: 100
    4. height: 100
    5. VisualItemModel {
    6. id: itemModel
    7. Rectangle { height: 30; width: 80; color: "red" }
    8. Rectangle { height: 30; width: 80; color: "green" }
    9. Rectangle { height: 30; width: 80; color: "blue" }
    10. }
    11.  
    12. ListView {
    13. id: view
    14. anchors.fill: parent
    15. model: itemModel
    16. focus: true
    17. Keys.onPressed: {
    18.  
    19. console.log(odump(view.model));
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    I get the following output:

    Qt Code:
    1. objectName:
    2. itemsRemoved: function
    3. itemsInserted: function
    4. count: 3
    5. modelReset: function
    6. countChanged: function
    7. childrenChanged: function
    8. createdItem: function
    9. itemsMoved: function
    10. destroyingItem: function
    11. children:
    To copy to clipboard, switch view to plain text mode 

    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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: children.count undefined

    I'd try declaring a new item (if it's possible from JavaScript at all) and setting the parent to be the visual model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Count files of a directory
    By radu_d in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2012, 04:17
  2. QThread count in Qt applications.
    By hashimov in forum Qt Programming
    Replies: 21
    Last Post: 22nd October 2010, 14:41
  3. Replies: 0
    Last Post: 17th March 2010, 13:17
  4. Replies: 7
    Last Post: 25th April 2009, 06:13
  5. [QGLWidget] Count FPS
    By Macok in forum Qt Programming
    Replies: 0
    Last Post: 13th April 2009, 15:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.