Hi Stefan, On Tuesday 23 February 2016 05:34:03 Walter Stefan wrote: [...] > > And in this way. I can use it then in the script: > value = sqrt(4); > > I actually don't want to map a whole QObject, because that would require a > call like this: value = MyMath.sqrt(4); > > Is there any way to achive in QJSEngine the same result as I do within the > QScriptEngine?
Is far as I know, the only way is to do the thing you don't want to: map the whole QObject. You can, however, add a JS function reference inside the engine so you can call the function without the object name. QJSEngine engine; engine.globalObject().setProperty( "MyMath", engine.newQObject( new ScriptModuleMath( &engine ) ) ); engine.evaluate( "var sqrt=MyMath.sqrt;" ); // repeat for other functions This will now work in JS: value = sqrt(4); You'll presumably be aware that all public slots of MyMath will automatically be available as JS functions. That makes things a little easier. It seems that you also need a QObject instance, event if you only want to expose static methods. BTW: your C++ native function can use this signature: double sqrt(double); You'll get NaN if somebody calls it with a non-number. The return value is also automatically converted as there is a QJSValue constructor that takes a double. There's no direct equivalent of read-only/undeletable, unfortunately. I'm missing that too. The closest you can get is that your MyMath functions can't be altered from JS, and you can also make a read-only Q_PROPERTY. However, there is nothing stopping somebody from reassigning properties of the global object - so your 'sqrt' function and 'MyMath' object could be replaced. This may be a security concern, depending on what you're doing. Best regards, Steve _______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest