Can I expose an already existing QMainWindow and access all its children from a .js script?
QtJSAPI usage
Moderator: andrew
Forum rules
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
- andrew
- Site Admin
- Posts: 8774
- Joined: Fri Mar 30, 2007 6:07 am
QtJSAPI usage
From a developer:
- andrew
- Site Admin
- Posts: 8774
- Joined: Fri Mar 30, 2007 6:07 am
Re: QtJSAPI usage
Yes. You can expose existing QObject objects as you normally would:
In JS, you can then use:
or for example:
Code: Select all
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);
Code: Select all
var w = appWin.findChild('NameOfChild');
Code: Select all
var children = appWin.children();
-
- Newbie Member
- Posts: 6
- Joined: Tue Jan 08, 2019 10:29 pm
Re: QtJSAPI usage
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:
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.
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.QObject Integration wrote:Use newQObject() to wrap a QObject (or subclass) pointer. newQObject() returns a proxy script object; properties, children, and signals and slots of the QObject are available as properties of the proxy object. No binding code is needed because it is done dynamically using the Qt meta object system.
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.