How to use QJSEngine to access a child of a QObject in js script?

  • From the official qt doc, it says:

    Signals and slots, properties and children of object are available as properties of the created QJSValue.

    So I expect to access the children of a QObject, but I do not find the right way to do it.

    I have code as below:

        QWidget parent;
        QLabel label(&parent);
        label.setText("Hello World");
        QJSEngine engine;
        QJSValue jsv = engine.newQObject(&parent);
        engine.globalObject().setProperty("w", jsv);
        qDebug() << engine.evaluate("JSON.stringify(w);").toString();
       // qDebug() << engine.evaluate("w.myLabel.text").toString(); // Does not work, how to access myLabel.text in script

    The debug output is as below, and I cannot find the child label information (eg. the text "Hello World") in this output. So, my question is how to access the QLabel (objectName: "myLabel") in js script?

        "objectName": "",
        "modal": false,
        "windowModality": 0,
        "enabled": true,
        "geometry": {},
        "frameGeometry": {},
        "normalGeometry": {},
        "x": 0,
        "y": 0,
        "pos": {},
        "frameSize": {},
        "size": {},
        "width": 640,
        "height": 480,
        "rect": {},
        "childrenRect": {},
        "childrenRegion": {},
        "sizePolicy": {},
        "minimumSize": {},
        "maximumSize": {},
        "minimumWidth": 0,
        "minimumHeight": 0,
        "maximumWidth": 16777215,
        "maximumHeight": 16777215,
        "sizeIncrement": {},
        "baseSize": {},
        "palette": {},
        "font": {},
        "cursor": {},
        "mouseTracking": false,
        "isActiveWindow": false,
        "focusPolicy": 0,
        "focus": false,
        "contextMenuPolicy": 1,
        "updatesEnabled": true,
        "visible": false,
        "minimized": false,
        "maximized": false,
        "fullScreen": false,
        "sizeHint": {},
        "minimumSizeHint": {},
        "acceptDrops": false,
        "windowTitle": "",
        "windowIcon": {},
        "windowIconText": "",
        "windowOpacity": 1,
        "windowModified": false,
        "toolTip": "",
        "toolTipDuration": -1,
        "statusTip": "",
        "whatsThis": "",
        "accessibleName": "",
        "accessibleDescription": "",
        "layoutDirection": 0,
        "autoFillBackground": false,
        "styleSheet": "",
        "locale": {},
        "windowFilePath": "",
        "inputMethodHints": 0

Log in to reply

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