Details
-
Suggestion
-
Status: Closed
-
Not Evaluated
-
Resolution: Moved
-
5.9.4
-
None
-
None
-
Windows 10, Chrome
Description
Hi,
I am using the qwebchannel.js provided in the Qt examples and connecting an existing Qt application to a web-page like it is done for the 'Qt WebChannel Standalone' example. I found several problems when documenting basic use cases:
1) returning a custom QObject* as a property
The qtwebchannel.js doesn't wrap the property as a QObject, so I propose to change the code as follow:
this.propertyUpdate = function(signals, propertyMap) { // update property cache for (var propertyIndex in propertyMap) { var propertyValue = propertyMap[propertyIndex]; object.__propertyCache__[propertyIndex] = this.unwrapQObject(propertyValue); } … }
2) in some cases we would like to retrieve the result of a slot synchronously so I propose to wrap the slot invocation in a Promise when there is no callback so that client code may use 'await'
function addMethod(methodData) { ... if (callback) { webChannel.exec({ "type": QWebChannelMessageTypes.invokeMethod, "object": object.__id__, "method": methodIdx, "args": args }, function (response) { if (response !== undefined) { var result = object.unwrapQObject(response); (callback)(result); } }); } else { var promise = new Promise(function (resolve, reject) { webChannel.exec({ "type": QWebChannelMessageTypes.invokeMethod, "object": object.__id__, "method": methodIdx, "args": args }, function (response) { if (response !== undefined) { var result = object.unwrapQObject(response); resolve(result); } else { resolve(); } }); }); return promise; } }
3) Passing a QObject as argument of a slot in a script
Assuming the QObject 'o' is already exposed to the scripting side, as a property of another QObject for ex. Calling slot(o) fails with the following error message "Cannot not convert non-object argument xxx to QObject*.", even though the QObject was published to javascript by Qt without problem. (all its properties, slots, can be seen in Chrome/Inspect)
This is because QMetaObjectPublisher looks for a value for the "id" field on the Json data provided from javascript and doesn't find it. In fact it is not symetrical with what qwebchannel.js is doing: the script fills a '_id_' field instead.
To test I modified the qwebchannel.js as follow:
function QObject(name, data, webChannel) { this.__id__ = name; this.id = name; // CC: QMetaObjectPublisher looks for KEY_ID = "id" when calling unwrapObject(value.toObject()[KEY_ID].toString()); ...