[Interest] QML id as string?

Mitch Curtis mitch.curtis at qt.io
Thu Oct 4 15:34:04 CEST 2018


Not sure if this is what you're after, but you can get an id as a string via http://doc.qt.io/qt-5/qqmlcontext.html#nameForObject:

"Returns the name of object in this context, or an empty string if object is not named in the context. Objects are named by setContextProperty(), or by ids in the case of QML created contexts.

If the object has multiple names, the first is returned."

> -----Original Message-----
> From: Interest <interest-bounces+mitch.curtis=qt.io at qt-project.org> On
> Behalf Of Jérôme Godbout
> Sent: Thursday, 4 October 2018 3:27 PM
> To: Jason H <jhihn at gmx.com>
> Cc: Qt Interest <interest at qt-project.org>
> Subject: Re: [Interest] QML id as string?
> 
> That would be a nice addition, have you try to see if the QQmlContext can
> still resolve a QQmlExpression http://doc.qt.io/qt-5/qqmlexpression.html
> with the given id? Maybe there would be a way to recover the ids form the
> context somehow and kind of reverse find the ids until you find your match
> into the actual context.
> 
> All QObject have his QQmlContext that can be recover with the QQmlEngine:
> http://doc.qt.io/qt-5/qqmlengine.html#contextForObject
> 
> 
> not sure it is possible, but it could worth an investigation to see if something
> can be extracted from there.
> 
> if you ever choose to accept this mission, we would like to have your finding
> on the subject ;-) Jerome
> 
> 
> On Thu, 4 Oct 2018 at 08:23, Jason H <jhihn at gmx.com
> <mailto:jhihn at gmx.com> > wrote:
> 
> 
> 	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" <godboutj at amotus.ca
> <mailto:godboutj at amotus.ca> >
> 	To: morten at winkler.dk <mailto:morten at winkler.dk>
> 	Cc: "Qt Interest" <interest at qt-project.org <mailto:interest at 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. <morten at winkler.dk
> <mailto: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 <mailto:Interest at qt-project.org>
> 		http://lists.qt-project.org/mailman/listinfo/interest
> 
> 
> 
> 	--
> 
> 
> 
> 
> 
> <https://docs.google.com/uc?export=download&id=1PzqBIgnmpnXWUhS9n
> kg1P3_-Ealbvl-
> X&revid=0B28h_MWkOCu2V2llWWs1M3gySUxQeVJFa3Q0Y3RxdkdtWjlzPQ>
> 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 <http://statum-iot.com/>
> 
> 
> 
> 	_______________________________________________
> Interest mailing list Interest at qt-project.org <mailto:Interest at qt-
> project.org>  http://lists.qt-project.org/mailman/listinfo/interest
> 
> 
> 
> --
> 
> 
> 
> 
> 
> 
> 
> <https://docs.google.com/uc?export=download&id=1PzqBIgnmpnXWUhS9n
> kg1P3_-Ealbvl-
> X&revid=0B28h_MWkOCu2V2llWWs1M3gySUxQeVJFa3Q0Y3RxdkdtWjlzPQ>
> 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 <http://statum-iot.com/>
> 
> 



More information about the Interest mailing list