有没有办法列出QML&问题5.1?
例如:
var obj=myQObject;
console.log(obj)
// expected output:
// obj { x:123..... }
这将对调试非常有帮助。
Straight javascript提供了您想要的东西:
JSON.stringify(anything)
它适用于矩形等QML项,也适用于大多数任意对象!
将对象转换为字符串
使用元对象,您可以调试任何QML obj
(即QQuickItem)的所有属性。
您需要一些C++来获取QML组件的元对象,并在QML中以文本形式返回属性名称和值。
首先用properties
方法在C++中创建一个QMLDebugger
类:
QString QMLDebugger::properties(QQuickItem *item, bool linebreak)
{
const QMetaObject *meta = item->metaObject();
QHash<QString, QVariant> list;
for (int i = 0; i < meta->propertyCount(); i++)
{
QMetaProperty property = meta->property(i);
const char* name = property.name();
QVariant value = item->property(name);
list[name] = value;
}
QString out;
QHashIterator<QString, QVariant> i(list);
while (i.hasNext()) {
i.next();
if (!out.isEmpty())
{
out += ", ";
if (linebreak) out += "n";
}
out.append(i.key());
out.append(": ");
out.append(i.value().toString());
}
return out;
}
这个函数可以是静态的,也可以是可实例化的,没关系。QML无论如何都不支持将静态方法从C++导出到QML。我使用标题:
public:
Q_INVOKABLE static QString properties(QQuickItem *item, bool linebreak = true);
现在将类导出到QML。在您的main.cpp
中添加
#include "qmldebugger.h"
和
qmlRegisterType<QMLDebugger>("MyDemoLibrary", 1, 0, "QMLDebugger");
在QML文件中导入新库,创建QMLDebugger实例并开始愉快的调试:
import QtQuick 2.0
import MyDemoLibrary 1.0
Rectangle {
id: mainRectangle
width: 360
height: 360
color: "silver"
Text {
id: textElement
color: "#d71f1f"
text: qsTr("Hello World")
font.bold: true
font.italic: true
font.underline: true
style: Text.Raised
horizontalAlignment: Text.AlignHCenter
font.pointSize: 16
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
}
QMLDebugger {
id: qmlDebugger
}
Component.onCompleted: {
console.log("Debug mainRectangle:");
console.log(qmlDebugger.properties(mainRectangle));
console.log("Debug textElement:");
console.log(qmlDebugger.properties(textElement, false));
}
}
完整的源代码可作为最低限度的Qt Creator项目提供:https://github.com/webmaster128/QMLDebugger
只需将QML/C++组件/对象转换为JavaScriptvar对象,并使用for each语法列出所有属性:
function listProperty(item)
{
for (var p in item)
console.log(p + ": " + item[p]);
}
在QML文件中,只需调用
onClicked:
{
listProperty(ItemID)
//or with this to list self properties
listProperty(this)
}
如果有人只想列出对象的属性,没有信号或插槽,你可以使用这个
function listProperty(item)
{
for (var p in item)
{
if( typeof item[p] != "function" )
if(p != "objectName")
console.log(p + ":" + item[p]);
}
}
如果您不仅对控制台调试感兴趣,KDAB有一个名为GammaRay的程序(链接),可以让您在基于QWidgets或QtQuick的程序运行时内省和更改所有属性。非常整洁!
我还没有看到迭代所有属性的解决方案。但这可能会帮助你迈出第一步。
对于每个快速项目,您可以打印出Item
:的属性
import QtQuick 2.0
Rectangle {
width: 360
height: 360
function debugQuickItem(object) {
var properties = {
'activeFocus': object.activeFocus,
'activeFocusOnTab': object.activeFocusOnTab,
'anchors.alignWhenCentered': object.anchors.alignWhenCentered,
'anchors.baseline': object.anchors.baseline,
'anchors.baselineOffset': object.anchors.baselineOffset,
'anchors.bottom': object.anchors.bottom,
'anchors.bottomMargin': object.anchors.bottomMargin,
'anchors.centerIn': object.anchors.centerIn,
'anchors.fill': object.anchors.fill,
'anchors.horizontalCenter': object.anchors.horizontalCenter,
'anchors.horizontalCenterOffset': object.anchors.horizontalCenterOffset,
'anchors.left': object.anchors.left,
'anchors.leftMargin': object.anchors.leftMargin,
'anchors.margins': object.anchors.margins,
'anchors.right': object.anchors.right,
'anchors.rightMargin': object.anchors.rightMargin,
'anchors.top': object.anchors.top,
'anchors.topMargin': object.anchors.topMargin,
'anchors.verticalCenter': object.anchors.verticalCenter,
'anchors.verticalCenterOffset': object.anchors.verticalCenterOffset,
'antialiasing': object.antialiasing,
'baselineOffset': object.baselineOffset,
'children': object.children,
'childrenRect.height': object.childrenRect.height,
'childrenRect.width': object.childrenRect.width,
'childrenRect.x': object.childrenRect.x,
'childrenRect.y': object.childrenRect.y,
'clip': object.clip,
'data': object.data,
'enabled': object.enabled,
'focus': object.focus,
'height': object.height,
'implicitHeight': object.implicitHeight,
'implicitWidth': object.implicitWidth,
'layer.effect': object.layer.effect,
'layer.enabled': object.layer.enabled,
'layer.format': object.layer.format,
'layer.mipmap': object.layer.mipmap,
'layer.samplerName': object.layer.samplerName,
'layer.smooth': object.layer.smooth,
'layer.sourceRect': object.layer.sourceRect,
'layer.textureSize': object.layer.textureSize,
'layer.wrapMode': object.layer.wrapMode,
'opacity': object.opacity,
'parent': object.parent,
'resources': object.resources,
'rotation': object.rotation,
'scale': object.scale,
'smooth': object.smooth,
'state': object.state,
'states': object.states,
'transform': object.transform,
'transformOrigin': object.transformOrigin,
'transitions': object.transitions,
'visible': object.visible,
'visibleChildren': object.visibleChildren,
'width': object.width,
'x': object.x,
'y': object.y,
'z': object.z,
}
var out = "{ "
for (var key in properties)
{
out += "'" + key + "': " + properties[key] + ", "
}
out += "}"
return out;
}
Text {
id: textObject
anchors.centerIn: parent
text: "Hello World"
}
Component.onCompleted: console.log(debugQuickItem(textObject));
}
输出:
{activeFocus:false,activeFocusOnTab:false,anchors.alignWhenCentered:true,anchors.c基线:QVariant(QQuickAnchorLine),anchor.sbaselineOffset:0,anchor.bottom:QVariation(QQuick锚线),anchos.bottomMargin:0,"horizontalCenterOffset":0,"anchors.left":QVariant(QQuickAnchorLine),"anchos.leftMargin':0,"anchors.margins":0,"baselineOffset":14,"children":[object object],"childrenRect.height":0,名称':来源,"layer.ssmooth":false,"layer.sourceRect":QRectF(0,0,0),"layer.textureSize":QSize(-1,-1),"layer.wrapMode":0,"opacity":1,"parent":QQuickRectangle_QML_0(0x29857d0),visible’:true,"visibleChildren":[object object],"width":80.5625,"x":139.71875,"y":171,"z":0,}