QObject children and dynamic properties in QJSEngine



  • According to QJSEngine documentation, I should be able to access "properties, children, and signals and slots of the QObject are available as properties of the proxy object" (QJSValue). However that doesn't seem to be the case.

    I created object obj and another (named) object child with obj as parent. I also set dynamic property of object obj. I can't access object's child and I can't access the dynamic property, as shown in the following snippet:

    @QJSEngine engine;
    auto *obj = new QObject;
    auto *child = new QObject(obj);
    child->setObjectName("child");

    auto sObj = engine.newQObject(obj);
    engine.globalObject().setProperty("obj", sObj);

    obj->setProperty("prop", 42);

    qDebug() << sObj.property("child").toString();
    qDebug() << sObj.property("obj").property("prop").toString();
    qDebug() << engine.evaluate("var x = ''; for (var p in obj) { x += p + ' '; }; x").toString();@

    The output is:
    @"undefined"
    "undefined"
    "objectName destroyed destroyed objectNameChanged deleteLater _q_reregisterTimers "@

    I would expect it to be some string representing child object in the first line (like for other objects), "42" in the second line and contain "prop" and "child" in the third line.

    Am I doing something wrong or, unlike in QtScript, you can't access children and dynamic properties of an object through JS properties?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.