[Qt-qml] Cannot access child elements within Component

3,505 views
Skip to first unread message

Krenar Qehaja

unread,
Feb 29, 2012, 6:17:01 PM2/29/12
to qt-qml
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

Bo Thorsen

unread,
Mar 1, 2012, 1:58:46 AM3/1/12
to qt-...@qt.nokia.com
I'm not completely certain of the scope rules in Component, but to me
this looks fishy. AFAIK, the items Component isn't actually instantiated
in that place, but rather in the delegate. I think of this as a class
declaration in C++ - you can't access an object by the classname either.
If this assumption is wrong, someone please correct me.

Instead, I'd assume you need to access it through the listview:

console.log(... + listView.delegate.items.height)

That's what I would try. But this is untested, so I might be completely
wrong here.

P.S. QML is still a pretty new beast, so even someone like me who makes
a living being a Qt expert still needs to learn some of the things
around it. That's why I write this mail instead of letting it go when I
don't know for sure. I want to see the discussions around subjects like
this, so I can learn more about the topics.

Bo.

> _______________________________________________
> Qt-qml mailing list
> Qt-...@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-...@qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-qml

Jason H

unread,
Mar 1, 2012, 7:51:30 AM3/1/12
to Krenar Qehaja, qt-qml
I think this is correct. Try aliasing to properties level component. That I think works. 

This is unfortunately an unfortunate limitation.


From: Krenar Qehaja <ked...@gmail.com>
To: qt-qml <qt-...@qt.nokia.com>
Sent: Wednesday, February 29, 2012 6:17 PM
Subject: [Qt-qml] Cannot access child elements within Component

Bo Thorsen

unread,
Mar 1, 2012, 7:58:33 AM3/1/12
to qt-...@qt.nokia.com
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:

> _______________________________________________
> Qt-qml mailing list
> Qt-...@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

Mark Tucker

unread,
Mar 1, 2012, 12:02:53 PM3/1/12
to qt-...@qt.nokia.com
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

Reply all
Reply to author
Forward
0 new messages