Is there any way to list all object members/properties in QML & Qt 5.1?
Such as:
var obj=myQObject; console.log(obj) // expected output: // obj { x:123..... }
This would be very helpful for debugging.
The QtObject type is a non-visual element which contains only the objectName property. It can also be useful for C++ integration, as it is just a plain QObject.
Attached Properties and Attached Signal Handlers In particular, they allow objects to access properties or signals that are specifically relevant to the individual object. A QML type implementation may choose to create an attaching type in C++ with particular properties and signals.
QProperty<T> is one of the classes implementing Qt Bindable Properties. It is a container that holds an instance of T. You can assign a value to it and you can read it via the value() function or the T conversion operator.
Emitted after component "startup" has completed. This can be used to execute script code at startup, once the full QML environment has been established. The corresponding handler is onCompleted. It can be declared on any object. The order of running the onCompleted handlers is undefined.
Straight javascript offers what you are looking for:
JSON.stringify(anything)
It works on QML items such as Rectangle, and it also works on most arbitrary objects!
Converting an object to a string
With meta objects you can debug all properties of any QML obj
(i.e. QQuickItem).
You need some C++ to get the meta object of a QML component and get back property names and values as text in QML.
First you create a QMLDebugger
class in C++ with a properties
method:
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; }
This function can be static or instanceiable, doesn't matter. QML does not support exporting static methods from C++ to QML anyway. I use the header:
public: Q_INVOKABLE static QString properties(QQuickItem *item, bool linebreak = true);
Now you export the class to QML. In you main.cpp
add
#include "qmldebugger.h"
and
qmlRegisterType<QMLDebugger>("MyDemoLibrary", 1, 0, "QMLDebugger");
In your QML file import you new library, create an instance of QMLDebugger and start happy debugging:
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)); } }
The full source code is available as a minimal Qt Creator project on: https://github.com/webmaster128/QMLDebugger
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With