[Development] QTCS2019 Notes from QtQml session

Damian Ivanov damianatorrpm at gmail.com
Fri Nov 22 18:04:18 CET 2019


Additionally https://forum.qt.io/topic/54353/solved-access-qml-singleton-object-from-c-code/4
seems also like a viable solution
though a syntax where qmlRegisterSingletonType return value is a
QObject would be very ease to read / write.

On Fri, Nov 22, 2019 at 6:33 PM Damian Ivanov <damianatorrpm at gmail.com> wrote:
>
> Hi Dmitriy,
>
> Well I can't seem to find an easy example but an easy example:
> Instantiating a .qml with QQmlComponent::create which is of Window (I
> do this for others as well) type
> from c++ than I set obj->properties from c++ based on various events.
> Creating a singleton class seems like adding 30 lines of code per
> object in contrast to setContextProperty()
> Something like this would be nice if possible:
>             QQmlComponent* m_component = new
> QQmlComponent(application_engine, "qrc:/Component.qml");
>             QObject* m_obj ;
>         qmlRegisterSingletonType<QObject*>("Component", 1, 0, "Identifier", [=]
>         {
>              m_obj = m_component->create();
>             return m_obj;
>         });
>
> On Fri, Nov 22, 2019 at 6:22 PM Dmitriy Purgin <dpurgin at gmail.com> wrote:
> >
> > Hi Damian,
> >
> > that seems to be an interesting case. Could you provide an example of why would you need such a pattern?
> >
> > Why the other way round -- creating a C++ singleton and exposing it to QML -- doesn't work for you? That should be easier and more transparent.
> >
> > Cheers
> > Dmitriy
> >
> > On Fri, Nov 22, 2019 at 4:50 PM Damian Ivanov <damianatorrpm at gmail.com> wrote:
> >>
> >> How would .qml files that are singletons and instantiated from c++ be used
> >> currently I do this and am able to access it from qml and c++
> >> QQmlComponent* component = new QQmlComponent(application_engine,
> >> "qrc:/Component.qml");
> >> QObject* obj = component->create();
> >> obj->setParent(this);
> >> application_engine->rootContext()->setContextProperty("obj", obj);
> >>
> >> I don't see this anywhere documented how this could be done using a singleton.
> >> If I register it as singleton the application_engine doesn't find it
> >> findChild->
> >> Using component->create would create it again since it is already
> >> registered as a singleton.
> >>
> >> Any suggestions?
> >>
> >>
> >> ;
> >>
> >> On Fri, Nov 22, 2019 at 12:53 PM Kai Uwe Broulik <kde at privat.broulik.de> wrote:
> >> >
> >> > Hi,
> >> >
> >> >  > these are the notes on the QtQml session:
> >> >
> >> >  > Versioning
> >> >  > ----------
> >> >  >
> >> >  > Once we get rid of unqualified lookups we don't need any QML versioning
> >> >  > anymore. At least the minor version will become optional already in 5.15
> >> >  > for well-behaved QML documents. In QML 3 it won't be allowed.
> >> >
> >> > While I agree with this overall, there's still one caveat which I
> >> > haven't seen discussed yet:
> >> >
> >> > Imagine I have the following code:
> >> >
> >> > Item {
> >> >      id: foo
> >> >      property color prettyColor: "#f0f"
> >> >
> >> >      Rectangle {
> >> >          color: foo.prettyColor
> >> >      }
> >> > }
> >> >
> >> > Right now "foo" refers to the Item. What if in a future update,
> >> > Rectangle gains a new property "foo". Unless you want to force everyone
> >> > to be explicit by using "this" (e.g. width: this.height) everywhere,
> >> > which would be annoying, I don't see how this can be made future-proof
> >> > without some form of versioning or some other magic solution?
> >> >
> >> > Cheers
> >> > Kai Uwe
> >> > _______________________________________________
> >> > Development mailing list
> >> > Development at qt-project.org
> >> > https://lists.qt-project.org/listinfo/development
> >> _______________________________________________
> >> Development mailing list
> >> Development at qt-project.org
> >> https://lists.qt-project.org/listinfo/development


More information about the Development mailing list