From a developer:
Can I expose an already existing QMainWindow and access all its children from a .js script?
From a developer:
Can I expose an already existing QMainWindow and access all its children from a .js script?
Yes. You can expose existing QObject objects as you normally would:
QJSEngine* engine = new QJSEngine();
RJSApi* rjsapi = new RJSApi(engine);
// this makes the Qt classes known to the script engine:
rjsapi->init();
QJSValue global = engine->globalObject();
global.setProperty("appWin", engine->newQObject(appWin));
QQmlEngine::setObjectOwnership(appWin, QQmlEngine::CppOwnership);
In JS, you can then use:
var w = appWin.findChild('NameOfChild');
or for example:
var children = appWin.children();
Thank you again for sharing QtJsApi
I’ve started looking at the mess that the QtScript to QJSEngine transition is.
Indeed the official documentation states:
However it does not seems to recurse on childrens.
I’m still learning but it looks like sub-objects exposed as Q_PROPERTY do work recursively like it used to do with QtScript, but sub-objects that are children are not seen from the JS side.
On another note it is still unclear to me if qScriptRegisterMetaType is still needed to register class of sub-object that are indirectly exposed and what would be the QJSEngine equivalent helper macro.
Qt should really have release a transition guide.