[Interest] How to get instance of qml singleton on C++ side
Лагнер, Сергей
developer at lagner.ru
Tue Jan 16 09:09:33 CET 2018
Thank you for advice.
Indeed, there is note about it in Qt documentation.
> NOTE: A QObject singleton type instance returned from a singleton type
provider
is owned by the QML engine unless the object has explicit
QQmlEngine::CppOwnership flag set.
But I can't use exactly that way. I need an instance of QQmlEngine in my
class.
I was looking for a method like the following for attached objects.
template<typename T> QObject *qmlAttachedPropertiesObject(const QObject
*attachee, bool create = true)
As I see, there is no such. May be I should develop my own.
2018-01-15 17:58 GMT+07:00 Pierre-Yves Siret <py.siret at gmail.com>:
> You could do an usual c++ singleton
>
> Foo& Foo::instance()
> {
> static Foo foo;
> return foo;
> }
>
> And use that in c++ and also in the singleton provider for QML :
>
> qmlRegisterSingletonType<Foo>("com.example", 1, 0, "Foo", &Foo::create);
>
> QObject* Foo::create(QQmlEngine*, QJSEngine*)
> {
> Foo* foo = &instance();
> QQmlEngine::setObjectOwnership(foo, QQmlEngine::CppOwnership);
> return foo;
> }
>
> 2018-01-15 7:35 GMT+01:00 Лагнер, Сергей <developer at lagner.ru>:
>
>> Hi
>>
>> It's exactly what I need.
>> I thought about such solution but compile new component,
>> create instance of it... seems like huge amount of extra work.
>> And it is kind of tricky. I was hoping the other way exists.
>>
>> But thank you anyway.
>>
>> 2018-01-14 16:41 GMT+07:00 Ben Lau <xbenlau at gmail.com>:
>>
>>>
>>> You may take this piece of code as an example:
>>>
>>> https://github.com/benlau/quickflux/blob/master/qfappdispatcher.cpp#L51
>>>
>>> On 14 January 2018 at 17:09, Лагнер, Сергей <developer at lagner.ru> wrote:
>>>
>>>> Hello all.
>>>> I have two classes registered as qml singletons.
>>>>
>>>>
>>>> QObject * First::create(QQmlEngine*, QJSEngine*) {
>>>> return new First();
>>>> }
>>>>
>>>> QObject * Second::create(QQmlEngine*, QJSEngine*) {
>>>> return new Second();
>>>> }
>>>>
>>>> qmlRegisterSingletonType<First>("com.example", 1, 0, "First",
>>>> &First::create);
>>>> qmlRegisterSingletonType<Second>("com.example", 1, 0, "Second",
>>>> &Second::create);
>>>>
>>>>
>>>>
>>>> Now I'd like to have an access to one of them from another.
>>>> For example
>>>>
>>>> void Second::method() {
>>>> // I need First * instance here
>>>> }
>>>>
>>>> Is there any way to get qml singleton instance on C++ side, from
>>>> QQmlEngine or something?
>>>>
>>>> ---
>>>> Have a nice day.
>>>> Sergey
>>>>
>>>> _______________________________________________
>>>> Interest mailing list
>>>> Interest at qt-project.org
>>>> http://lists.qt-project.org/mailman/listinfo/interest
>>>>
>>>>
>>>
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180116/d8a9b598/attachment.html>
More information about the Interest
mailing list