从官方 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
调试输出如下所示,但我在此输出中找不到子标签信息(例如文本“Hello World”)。所以,我的问题是如何在 js 脚本中访问 QLabel (objectName: "myLabel")?
{
"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
}