Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    QObject children and dynamic properties in QJSEngine

    General and Desktop
    1
    1
    705
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • X
      Xilexio last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • First post
        Last post