Create a global object
-
Sorry, didn't realize you are talking about scripting. Haven't played with QML that much, bit if things are inherited from the QtScript module, it should go something like this:
@engine.globalObject().setProperty("myObject", engine.newQObject(object));@
After that, your object will be available to qtscript.
As for making it pluggable with both c++ and js, I can't really help. (I could think of a dirty way to achieve it, but IMO it is better to wait for our friendly Trolls to tell us the proper way)
-
Thanks.
They do it in Qt Declarative code (QDeclarativeScriptEngine constructor in QDeclarativeEngine code)
I've an access to QDeclarativeEngine which use a QScriptEngine (internaly) on plugin init function.
But i don't find how to access to a QScriptEngine. -
You could try using setContextProperty() on the root context of the engine -- this will effectively add the object as a context property that is available to all the elements in the engine. From a plugin, you can do so inside the "initializeEngine()":http://doc.trolltech.com/main-snapshot/qdeclarativeextensionplugin.html#initializeEngine function.
I believe there are plans to look at this more for a future release, so plugins can easily add "namespaces" like the Qt object with functions, enum values, etc.
-
With this method, how create QScriptValue with newXXXX function ?
"http://doc.qt.nokia.com/4.7-snapshot/qscriptengine.html":http://doc.qt.nokia.com/4.7-snapshot/qscriptengine.htmlAnother solution that i will test, it's create a component with QDeclarativeComponent
"http://doc.qt.nokia.com/4.7-snapshot/qdeclarativecomponent.html":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativecomponent.htmlMake @import "file.js" as myObject@ equivalent in qmldir file could be interesting.
[edit: fixed hyperlink / $chetakjain ]
-
[quote author="yan_" date="1283758523"]With this method, how create QScriptValue with newXXXX function ?[/quote]
With the setContextProperty() approach, you would use a QObject with slots or Q_INVOKABLEs for your "global" object, rather than a QScriptValue. The Minehunt demo shows this approach (adds a MinehuntGame object in the root context).
-
[quote author="mbrasser" date="1283814387"]With the setContextProperty() approach, you would use a QObject with slots or Q_INVOKABLEs for your "global" object, rather than a QScriptValue. The Minehunt demo shows this approach (adds a MinehuntGame object in the root context).[/quote]
Thanks.
I've ever test it and it's work perfectly ^^I find a solution for JS code :D
1- create a qml file wich define function on the root element (I use QtObject)
2- in initializeEngine(), use a"QDeclarativeComponent ":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativecomponent.html.
3- set qml file path or use setData
4- use create to make a QObject
5- add this QObject with setContextPropertyLike QDeclarativeComponent use qml file, i think it's so possible to mix JS and C++ code
-
It's really impressive, thanks for that !