Thanks Jérôme.
It would be important to note, that what I am trying to do would be subject to a few (reasonable) caveats. It's not a find-anything-anywhere, definitely-uniique-id, it would be an approach that can be leverage if the situation makes it possible to get objects by id, rather than having to maintain it myself. This mapping already exists at some level, I just want it exposed.
Sent: Wednesday, October 03, 2018 at 1:31 PM
From: "Jérôme Godbout" <godbo...@amotus.ca>
To: mor...@winkler.dk
Cc: "Qt Interest" <interest@qt-project.org>
Subject: Re: [Interest] QML id as string?
From: "Jérôme Godbout" <godbo...@amotus.ca>
To: mor...@winkler.dk
Cc: "Qt Interest" <interest@qt-project.org>
Subject: Re: [Interest] QML id as string?
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. <mor...@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@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
|
|
_______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest