PDA

View Full Version : interacting C++ QML



kickoune
22nd September 2016, 14:42
hello,
i would like understand qt, so i post here.

rmq: QT : 5.7


if i setProperty class in my qml.
in this class, i have method Q_INVOKABLE what we return a pointeur in class.

like this with no var in my qml:

if (toto.test()!=NULL)
toto.test().x() //method Q_INVOKABLE x()

work but , the qml want manage the cobject returned by test()

if i quit the qml page.
qt would like delete the object returned by test() why??

i would like juste interacting C++ and QML.

what is the best method for use this? (no manage memory who i return in my q_INVOKABLE function)


thank's for your reply ;)

wysota
22nd September 2016, 16:19
You can use QQmlEngine::setObjectOwnership() with QQmlEngine::CppOwnership on a given object to specify it should be owned by C++ side.

kickoune
23rd September 2016, 10:44
thank's to your reply

Objects not-created by QML have CppOwnership by default.
The exception to this are objects returned from C++ method calls; their ownership will be set to JavaScriptOwnership. This applies only to explicit invocations of Q_INVOKABLE methods or slots, but not to property getter invocations.
Calling setObjectOwnership() overrides the default ownership heuristic used by QML.

getter invocations = Q_PROPERTY() ?

after Q_INVOKABLE Method in QML, i need to invok a method setObjectOwnership?

it's bad way?

Added after 45 minutes:

if i just connect signal, qml manage this object.... i don't want this....

getObj().signal.connect(fctQml)

Added after 47 minutes:

i have pass my pointer to setProperty, qt no manage memory, i think.

but in slot function QML, sometimes variable who is setProperty in main() is no visible. why???

kickoune
23rd September 2016, 11:31
solved: i must use Connection ==> it's work.

anda_skoa
23rd September 2016, 14:43
Objects not-created by QML have CppOwnership by default.
The exception to this are objects returned from C++ method calls; their ownership will be set to JavaScriptOwnership. [B]This applies only to explicit invocations of Q_INVOKABLE methods or slots, but not to property getter invocations.

Instances of QObject derived classes returned from Q_INVOKABLE methods or slots are assumed to be owned by C++ if they have a parent and assumed to be owned by QML if they do not.
Explicitly setting ownership takes precedence of course.

A property getter is never invoked by the QML engine, so there is never any need to use either assumption.
Property values are always owned by the object exposing the property.



after Q_INVOKABLE Method in QML, i need to invok a method setObjectOwnership?

Yes, if you want a different behavior than the one based on parent check.



it's bad way?

No, why would that be a bad way?



but in slot function QML, sometimes variable who is setProperty in main() is no visible. why???
What?

Cheers,
_

wysota
23rd September 2016, 21:21
but in slot function QML, sometimes variable who is setProperty in main() is no visible. why???
You need to set context properties before you load the QML document.