qjsengine

How to build an API with QJSEngine?

余生长醉 提交于 2019-12-13 12:15:30
问题 I am starting with Qt and one of my projects is using QJSEngine to evaluate javascript and I want to provide an entire API to the script, with classes and global functions. Right now my program provides only the ECMAScript default stuff (eval, encodeURI, parseInt, etc...), but I need to expose some custom classes to the code, like the browsers API (WebSocket class, Image class, document object). For example: var obj = new CustomClass("", 0); var ret = obj.customClassMethod("[...]!");

QJSEngine: print to console

江枫思渺然 提交于 2019-12-11 20:23:15
问题 I'm moving from QScriptEngine (which is deprecated) to QJSEngine , and I see that I'm unable to use print : QJSEngine engine; QJSValue val = engine.evaluate( "print('123');" ); if (val.isError()){ qDebug() << "error: " << val.toString(); } qDebug() << "val: " << val.toVariant(); The output is: error: "ReferenceError: print is not defined" In QScriptEngine it works. Then, what is the way to print something to console in QJSEngine ? Can't find anything in docs. I tried to use console.log , but

QJSEngine deletes my QObject, how to change ownership after QJSEngine::newQObject?

不打扰是莪最后的温柔 提交于 2019-12-10 15:09:18
问题 Trying to perform small script with subclassed QObject as parameter. QJSEngine jsEngine; QJSValue arg = jsEngine.newQObject(child); // Child it's subclassed QObject QJSValue function = jsEngine.evaluate(m_childRestriction); QJSValue result = function.call(QJSValueList() << arg); On destroying jsEngine, it calls delete for my child object (as newQObject creates it with JavaScriptOwnership). How to avoid it, how to change ownership for arg? Script is simple: function(device) { return device.m

How to build an API with QJSEngine?

為{幸葍}努か 提交于 2019-12-04 17:00:14
I am starting with Qt and one of my projects is using QJSEngine to evaluate javascript and I want to provide an entire API to the script, with classes and global functions. Right now my program provides only the ECMAScript default stuff (eval, encodeURI, parseInt, etc...), but I need to expose some custom classes to the code, like the browsers API (WebSocket class, Image class, document object). For example: var obj = new CustomClass("", 0); var ret = obj.customClassMethod("[...]!"); customFunction(ret); I need to define the behavior of the classes in C++, it wouldn't help evaluate the classes