[Interest] QJSengine access QObject children

Eric Lemanisser eric.lemanissier at gmail.com
Thu Oct 11 12:15:01 CEST 2018


I'm starting to use QJSEngine in order to make a full QObject hierarchy
scriptable. Calling QJSEngine::newQObject gives access to signals, slots
and properties of the object from the javascript side, but I could not find
the QObject's children in any properties, contrary to what is described in
the documentation http://doc.qt.io/qt-5/qjsengine.html#newQObject :
"Signals and slots, properties *and children* of object are available as
properties of the created QJSValue.". Did I miss something ?

As a workaround I ended up developing the following Q_INVOKABLE member,
which properly returns an array of objects with properties when invoked
from JS side:

QJSValue ScriptWindow::findChildren(const QJSValue &v)


    const auto engine = qjsEngine(this);

    const auto o = v.toQObject();

    if(!o || !engine) return {};

    const auto children = o->findChildren<QObject *>();

    auto res = engine->newArray(children.size());

    for(int i = 0; i < children.size(); i++)


        res.setProperty(i, engine->newQObject(children.at(i)));


    return res;


Shouldn't this boilerplate be integrated in Qt, with the handling of
QObject::findChildren parameters (QString aName and Qt::FindChildOptions) ?
dito for QObject::findChild

Eric Lemanissier
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20181011/d924bc3c/attachment.html>

More information about the Interest mailing list