Assuming that the height you require is that of the current Item when
you click/press a key, you can use the ListView's "currentItem" property
to obtain a reference to the current item. So youre console.log
statement would be:

console.log("listView height: " + listView.currentItem.height)

Attempting to access the id "items" in the context in which you are
doing so will cause the error you are seeing because "items" doesn't
exist in that context. You are in fact unable to refer to delegate items
of lists, etc. by id.

Mark

-----Original Message-----
From: qt-qml-bounces+mark.tucker=airborne.a...@qt.nokia.com
[mailto:qt-qml-bounces+mark.tucker=airborne.a...@qt.nokia.com] On Behalf
Of Bo Thorsen
Sent: 01 March 2012 12:59
To: qt-qml@qt.nokia.com
Subject: Re: [Qt-qml] Cannot access child elements within Component

I just re-read this. There is no way you can access one of these items
directly by using the id. That's because there can be any amount of them
in the ListView. So which height do you want to get? That's an
impossible question.

What you can do is to iterate over the children attribute of the
ListView. That's the only way I can see to do this.

Bo.

Den 01-03-2012 00:17, Krenar Qehaja skrev:
> Hi everyone,
>
> I cannot seem to access the child elements within a Component.
>
> I can access them through JavaScript though, if I create a function 
> and call it when one of the root elements gets loaded, but not after.
>
> Below is a snippet of code which illustrates my point:
> import QtQuick 1.0
>
> Rectangle {
>      id: base
>      color: "transparent"
>      width: 500
>      height: 500
>
>      // this function will be used to get the height of 'items', it 
> works fine
>      function getHeight(item1) {
>          console.log("items height using getHeigh function:
"+item1.height)
>      }
>
>      // listview
>      ListView {
>          id: listView
>          width: parent.width; height: parent.height
>          spacing: 5
>          model: fruitModel
>          delegate: fruitDelegate
>          focus: true
>          Keys.onPressed: { // listen for any key
>              console.log("listView height: " + listView.height); // 
> Try to get 'listView' Heigh, it works
>
>              console.log("items height: " + items.height);  // Try to 
> get 'items' height, this does not work.
>                                                           // This 
> error will appear in the console: Can't find variable: items
>                                                           // I should 
> be able to access the items.height from here, or access fruitDelegate 
> children from here
>                                                           // or try 
> with console.log("items height: " + getHeight(items));
>          }
>      }
>
>      ListModel { // Some list
>          id: fruitModel
>          ListElement {
>              name: "Apple"
>              cost: 2.45
>          }
>          ListElement {
>              name: "Orange"
>              cost: 3.25
>          }
>      }
>
>      Component { // Delegate
>            id: fruitDelegate
>            Row {
>                id: items
>                height: 15
>                spacing: 10
>                Text { text: name }
>                Text { text: '$' + cost }
>                Component.onCompleted: {
>                    getHeight(items); // I can access the height 
> property of 'items' when I pass the 'items' id to the getHeight
function
>                }
>            }
>      }
> }
>
> When Component loads, it calls getHeight function and it works just 
> fine. But when you press any key, you'll see an error complaining 
> about items.height.
>
> So, I don't know if this is a bug or it was meant that way but I 
> really do need to access those child elements.
>
> Cheers,
> Krenar
>
>
> _______________________________________________
> Qt-qml mailing list
> Qt-qml@qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-qml


Bo Thorsen,
Fionia Software.

-- 

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk
_______________________________________________
Qt-qml mailing list
Qt-qml@qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-qml
_______________________________________________
Qt-qml mailing list
Qt-qml@qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-qml

Reply via email to