[Interest] QML id as string?

Jérôme Godbout godboutj at amotus.ca
Wed Oct 3 19:31:59 CEST 2018


In that example, the Repeater create a new context (QQmlContext
http://doc.qt.io/qt-5/qqmlcontext.html ) per instance of the QQmlComponent.
That context inherite the context of the repeater. But your
Component.onCompleted is the same context as the Repeater but doesn't have
the context of the repeated childs. This is why it cannot find the myText
id and that why each child (with their own context) id can be used without
crosstalk with each others.

Side note: You can create your own object creation with Component as input
property and generating the QQmlContext and the Component instance into it
and make a new kind of repeater (I did myself a map of component and list
of name that generate different kind of object that way
(FactoryInstanciator).


On Wed, 3 Oct 2018 at 13:26, Morten W. J. <morten at winkler.dk> wrote:

> On onsdag den 3. oktober 2018 19.01.33 CEST Jason H wrote:
> > The lexical scoping rules are clear,
>
> What would you expect to be the output of the below?
>
> import QtQuick 2.9
> import QtQuick.Window 2.2
>
> Window {
>     visible: true
>     width: 640
>     height: 480
>     title: qsTr("Hello World")
>
>
>     Column {
>         Repeater {
>             model: 10
>             Text { id: myText; text: "I'm item " + index }
>         }
>         Text { id: last; text: "Last"}
>     }
>
>     Component.onCompleted:
>     {
>         console.log(myText.text);
>         console.log(last.text);
>     }
> }
>
> On my machine, the output is
>         qrc:/main.qml:21: ReferenceError: myText is not defined
> which is a great example on why you cannot rely on id's as item names.
> The id is only unique at type time.
>
>
> Cheers,
>    Morten
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>


-- 



RAPPROCHEZ LA DISTANCE


*Jérôme Godbout*Senior Software Developer

*p:* +1 (418) 800-1073 ext.:109

*m:* +1 (581) 777-0050

amotus.ca <http://www.amotus-solutions.com/>
statum-iot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20181003/3dc0e6d2/attachment.html>


More information about the Interest mailing list