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: 9063
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: 9063
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();

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”