QtJSAPI usage

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
User avatar
andrew
Site Admin
Posts: 8774
Joined: Fri Mar 30, 2007 6:07 am

QtJSAPI usage

Post by andrew » Mon Nov 13, 2023 10:45 am

From a developer:
Can I expose an already existing QMainWindow and access all its children from a .js script?

User avatar
andrew
Site Admin
Posts: 8774
Joined: Fri Mar 30, 2007 6:07 am

Re: QtJSAPI usage

Post by andrew » Mon Nov 13, 2023 10:54 am

Yes. You can expose existing QObject objects as you normally would:

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);
In JS, you can then use:

Code: Select all

var w = appWin.findChild('NameOfChild');
or for example:

Code: Select all

var children = appWin.children();

neFAST
Newbie Member
Posts: 6
Joined: Tue Jan 08, 2019 10:29 pm

Re: QtJSAPI usage

Post by neFAST » Fri Sep 12, 2025 8:22 am

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:
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.
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.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”