Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML: List all object members/properties in console

Tags:

qt

qt5

qml

qtquick2

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.

like image 815
tirth Avatar asked Nov 29 '13 21:11

tirth


People also ask

What is QObject in QML?

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.

What is attached properties in QML?

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.

What is QProperty in Qt?

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.

What is component onCompleted in QML?

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.


2 Answers

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

like image 147
Kevin Avatar answered Oct 11 '22 06:10

Kevin


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

like image 23
Simon Warta Avatar answered Oct 11 '22 07:10

Simon Warta