PDA

View Full Version : QtQuick 2 child list



Talei
18th August 2013, 18:24
Hello,
I have on QML few visual items, but some of them are added from within C++ code.
Now I need to access them from within QML on i.e. resize event.

I tried children.lenght but this is undefined.

Is there a way to iterate in QML over items children?

wysota
18th August 2013, 21:06
If you wish to run imperative code from within QML as a result of some event then I think your approach is not that much correct.

Talei
18th August 2013, 22:18
Thanks for the reply.
It was actually typo:

children.lenght vs children.length
Code that is run there only change item width, exactly width of combobox popup after rescale.

PS. Reason why I iterate over children's is that I add items from within C++, and it's object names comes from different place - external file, so it's easier for me to just iterate then actually change it from C++ because I don't keep track of them, only find parent during add operation.

wysota
19th August 2013, 07:37
Code that is run there only change item width, exactly width of combobox popup after rescale.
Why not use a binding?


PS. Reason why I iterate over children's is that I add items from within C++
You can still bind properties to expressions.

Talei
19th August 2013, 12:23
Because It's large application and I want to remove from c++ as much as I can.
QML pages content is changeable dynamically and I don't want to use binding just to change items width on resize event.

wysota
19th August 2013, 14:01
QML pages content is changeable dynamically and I don't want to use binding just to change items width on resize event.

That's one of the reasons why you'd want to set a binding.

Talei
19th August 2013, 17:03
Is it worth doing that just to change the width for 1 object?

wysota
19th August 2013, 19:41
I have no idea whether it is "worth" or not. I only know that if you use a declarative environment, you should program in a declarative way. At some point you might want to change something in your code and your imperative code might interfere with your changes causing you headache, nausea and other side effects. There is a Binding element in QML that does exactly what you want. However having read the thread again I think your initial problem might be that you interfere with GUI QML code from C++. Usually you'd do it the other way -- expose objects in C++ and then use them in QML. "Pulling" QML objects into C++ is considered bad practice.