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.
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
> [email protected]
> 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
[email protected]
http://lists.qt.nokia.com/mailman/listinfo/qt-qml