Now that I'm finally porting our Qt4 code to Qt5 I need to solve this issue somehow.
to QJS[X] seems to be mostly straightforward stuff. Still, solution to the original problem with integrating my custom extensions to QML applications isn't obvious. Could someone elaborate the stuff a bit?
I don't believe our need is unique in any way: we just need to be able to use our JavaScript extensions from both JavaScript and QML. I'm really surprised how difficult it really is to extend JavaScript this way in Qt5. It may be due to the lack of documentation, and I may certainly have missed something, but this is how I see it:
QtScript seems to be phased out, and it isn't compatible with QML anyway.
There is no documented extension mechanism for QJSEngine.
There is an extension mechanism for QML, but the JavaScript global object is read-only.
The simple question is: how do I add my own JavaScript extensions to QML applications?
This is already a bit out of topic, but I have to admit I'm bit lost regarding the integration of C++ and QML code. I cannot see QJSValue appear anywhere in QML documentation. Does this mean QML has its own set of reflection objects to the C++ side? Such as QQmlProperty?
Let's assume I have this QML code:
@
// main.qml
import QtQml 2.0
QtObject
{
id: test
function test() {}
}
@
Assume I have created an object out of the "test" component:
@
QQmlEngine engine;
QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
QObject* test = component.create();
@
Now I have a QObject pointer in C++.
How do I iterate the properties and functions the QML object has?
How do I add another function, say test2() to the object?
What about adding properties?
How do I call test()?