如何在js脚本中使用QJSEngine访问QObject的子代码?

时间:2017-03-31 01:52:06

标签: javascript qt qml qjsengine

来自official qt doc,它说:

  

对象的信号和槽,属性和子元素可用作创建的QJSValue的属性。

所以我希望能够访问QObject的子代,但我找不到正确的方法。

我的代码如下:

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

调试输出如下所示,但我在此输出中找不到子标签信息(例如文本&#34; Hello World&#34;)。所以,我的问题是如何在js脚本中访问QLabel(objectName:&#34; myLabel&#34;)?

{
    "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
}

0 个答案:

没有答案