[Development] Porting code from QScriptEngine (Qt4) to QJSEngine (Qt5)

christopher.adams at nokia.com christopher.adams at nokia.com
Mon Jun 25 09:32:26 CEST 2012


Hi Andrea,

My response doesn't relate to EnvJs in particular, although it may be relevant.

In general, support for JavaScript in QML is still lacking (examples: instanceof doesn't work properly, due to the dichotomy between C++ inheritance and JS prototypical inheritance and constructor functions; we don't allow modification of the evaluation context of a script import; etc) and this is probably going to be one of the focuses of QtDeclarative development for 5.1.  Most of those improvements are, however, out of scope for 5.0 due to time limitations.

You can do something like:

// main.qml
Import QtQuick 2.0
Import "Compat.js" as JS

Item {
     Component.onCompleted: console.log(JS.someFunction());
}

// Compat.js
Var Window = { "test": 42 };
Qt.include("thirdParty.js");

// thirdParty.js
Function someFunction() {
    Return Window.test;
}


When the main.qml is run with qmlscene, we see "42" printed out as expected.
In this way, third party JavaScript libraries can be made to work, with some limitations (as described previously).

Michael Brasser has written some documentation about the limitations of the JavaScript environment in QML, see http://qt.gitorious.org/qt/qtdeclarative/blobs/master/src/qml/doc/src/javascript/hostenvironment.qdoc for more information.

Cheers,
Chris.

> -----Original Message-----
> From: development-bounces+christopher.adams=nokia.com at qt-
> project.org [mailto:development-
> bounces+christopher.adams=nokia.com at qt-project.org] On Behalf Of ext
> a.grandi at gmail.com
> Sent: Monday, June 25, 2012 5:14 PM
> To: Stephen Kelly
> Cc: development at qt-project.org
> Subject: Re: [Development] Porting code from QScriptEngine (Qt4) to
> QJSEngine (Qt5)
> 
> Hi,
> 
> On 21 June 2012 17:18, Stephen Kelly <stephen.kelly at kdab.com> wrote:
> > On Thursday, June 21, 2012 15:33:19 a.grandi at gmail.com wrote:
> >> I've started porting some code from QScriptEngine to QJSEngine, but
> >> there are some missing parts I don't know how to implement.
> >
> > My understanding is that QJS* is not a replacement for QScriptEngine:
> >
> > http://thread.gmane.org/gmane.comp.lib.qt.devel/4185
> >
> > There's nothing for you to port to yet. Maybe there will be later in
> > Qt 5 development.
> 
> I've just read all the thread, maybe it's better if I provide more details about
> what I'm doing.
> 
> My current task is experimenting the compatibility of Qt5/QML JavaScript
> engine with the most popular JavaScript libraries (for
> example: jQuery, BackBoneJs, etc...).
> All these libraries need browser objects like "window", "document"
> etc... to work properly.
> 
> I found the EnvJs library and this useful blog article written by Kent Hansen
> http://labs.qt.nokia.com/2011/03/10/say-hello-to-envjs-for-qtscript/
> 
> the only problem is that he doesn't provide a complete (ready to be
> built) code example and he probably tested this on Qt4 (while I have to use
> Qt5), so I wrote a Qt5 example from scratch, trying to follow the article
> advices, but it's not working. I've tried to use both QtScriptEngine and
> QJSEngine but it doesn't work.
> 
> Basically:
> 
> 1) I load the env.js content and I evaluate it: qse.evaluate(js, "js_errors.txt");
> 
> 2) I call the initializeEnvjsNatives(&qse);
> 
> 3) from my main.qml I import the env.js file (I should not import it since I've
> already evaluated it with qse.evaluate(....) right? Anyway it doesn't work in
> both ways...
> 
> import "env.js" as EnvJs
> 
> 4) I try to use it in this way:
> 
> console.log("document:", EnvJs.document);
> EnvJs.document.write("<p>Hello, JavaScript in QtScript!</p>");
> 
> and this is the error I always get:
> 
> document: undefined
> file:///home/andrea/Documents/EnvjsTest/qml/EnvjsTest/main.qml:24:
> TypeError: Cannot call method 'write' of undefined
> 
> If it can be useful for you to understand better, I think I can provide the
> complete source code (it's just a basic example, but at least it will save you lot
> of time if you want to give it a try).
> 
> Best regards,
> 
> --
> Andrea Grandi - Nokia-DXM/Tampere / Qt Ambassador Ubuntu Member:
> https://launchpad.net/~andreagrandi
> website: http://www.andreagrandi.it
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development



More information about the Development mailing list