Q_INVOKABLE cpp method and passing attributes by reference from qml
-
Hi!
I would like to work with some parameters in qml and modify them in cpp by function.
The thing is that, I can accept them into an array of QObjects but I would like to pass them as parameters by reference.
For now, the function returns a boolean and accepts a string and a list of strings.
This would be the function:bool doSomething(QString strParameterOne, QSharedPointer<QVariantList> parameterTwo)
Qml says: Error: Unknown method parameter type: QSharedPointer<QVariantList>
I would like to be able to modify the parameters in cpp and see the changes in qml.Is this possible? Or the only way is to accept them in the return of the method?
-
@ars1614 said:
Is this possible? Or the only way is to accept them in the return of the method?
I don't develop in QML, but I really, really, really doubt you can pass parameters by reference between the two. As far as I know QML has some internal threading and passing by reference would ruin the Qt developers' day (i.e. it basically would be a ticking race-condition bomb).
-
@ars1614 To add to @kshegunov's answer check this: Using QVariantMap to Emulate Passing by Reference and Pointer in QML.
-
@p3c0 This is the way to 'emulate' a JSObject isn't it?
Another question that has come to me since your answer is If I have a JSObject like the one is written in the blog post and If I accept a QVariantMap in cpp, will it be automatically converted? -
@ars1614 Yes they are converted automatically.
http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qvariantlist-and-qvariantmap-to-javascript-array-and-object