[Interest] QML id as string?

Jérôme Godbout godboutj at amotus.ca
Thu Oct 4 17:06:31 CEST 2018


After a quick test, it seem like it will return the first nearer to the
root, so

A.qml
Item
{
   id: inner_
   function getObjName() { return MyCppSingleton.getName(inner_); }
}

main.qml
Window
{
   A { id: outter_ }

  Component.onCompleted:
  {
    console.log(MyCppSingleton.getName(outter_ );
    console.log(outter_ .getObjName());
  }
}

in both log case I end up with (at least with 5.10.1):
"outter_"
so it seem like it does the right thing, the description was a bit
confusing on this, but the code seem to do the right thing here.

On Thu, 4 Oct 2018 at 10:40, Jason H <jhihn at gmx.com> wrote:

> In my own immediate case, first is fine, but the observation is correct -
> the most useful one is actually the last.
>
> I'll have a look at nameForObject and let you know.
>
>
> *Sent:* Thursday, October 04, 2018 at 10:31 AM
> *From:* "Jérôme Godbout" <godboutj at amotus.ca>
> *To:* mitch.curtis at qt.io
> *Cc:* "Jason H" <jhihn at gmx.com>, "Qt Interest" <interest at qt-project.org>
> *Subject:* Re: [Interest] QML id as string?
> Nice, I will have to check this function and maybe we can add one that
> will return a QStringList with a maximum number of ids, because the second
> one is often the needed one (the first is the one used into the component
> file or .qml file if you prefer) and the second one is the id used when
> using that component into another file and this is what we want to know
> which one (at least for myself, not sure this is Jason use case).
>
>
> On Thu, 4 Oct 2018 at 09:34, Mitch Curtis <mitch.curtis at qt.io> wrote:
>
>> 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/>
>> >
>> >
>>
>
>
>
> --
>
>
>
>
> 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
>
>
>


-- 



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/20181004/409ab394/attachment.html>


More information about the Interest mailing list