Pass a QML Object to workerScript
-
Hi,
I have registered a type called 'Client' defined in C++ :
qmlRegisterType<Client>("com.mycompany.client", 1, 0, "Client");
I can Instantiate a Client{ } in QML and call Q_INVOKABLES read Q_PROPERTIES etc...
Now my client has a method witch takes 2-3 secondes, and causes UI sreezes, so i want to call that methode from a WorkerScript.
How can a pass 'myClient' to my WorkerScript ?
Client { id:myClient } Button{ onClicked: { var msg = {'client':myClient }; connect_worker.sendMessage(msg) // here i want to pass myClient to Connect_Worker.js } } WorkerScript{ id:connect_worker source: "Connect_Worker.js" }
//Connect_Worker.js
WorkerScript.onMessage = function(msg) { var co = msg.client.connectToSftpServer() return co }
Error : TypeError: Cannot call method 'connectToSftpServer' of undefined
How to correctly pass a QML Object to workerScript ?
Thank you in advance.
-
See the documentation of
WorkerScript.sendMessage()
: http://doc.qt.io/qt-5/qml-workerscript.html#sendMessage-method :The
message
object may only contain values of the following types:- boolean, number, string
- JavaScript objects and arrays
- ListModel objects (any other type of QObject* is not allowed)
All objects and arrays are copied to the message. With the exception of ListModel objects, any modifications by the other thread to an object passed in message will not be reflected in the original object.
Unfortunately, you cannot pass an arbitrary QML object or C++ QObject to WorkerScript.
One possible solution is for QML to send a signal back to C++ and create the new thread in C++.
@LeLev said in Pass a QML Object to workerScript:
Now my client has a method witch takes 2-3 secondes, and causes UI sreezes, so i want to call that methode from a WorkerScript.
Make sure your
Client
object is thread-safe!