Get properties list from Item
Solved
QML and Qt Quick
-
Hi,
I want to get a list of property names for a given QML Item. In short, I have an object in which I have created several properties, I want to create a for loop where I will iterate through all created properties. Is this even possible? In the snippet, sample code that can serve as a reference point:import QtQuick import QtQuick.Controls Window { width: 640 height: 480 visible: true title: qsTr("Hello World") Rectangle { id: rectangle property bool property1: false property int property2: 0 property var property3: ({}) width: 50 height: 50 color: "red" anchors.centerIn: parent } Button { width: 100 height: 50 text: qsTr("Click me!") anchors { horizontalCenter: parent.horizontalCenter topMargin: 10 top: rectangle.bottom } onClicked: { //for property in rectangle.properties // prints: property1, property2, property3 // } } }
-
I found a solution:
onClicked: { Object.keys(rectangle).forEach((key) => { if (rectangle.hasOwnProperty(key) && typeof rectangle[key] !== "function") { console.log(key + ": " + rectangle[key]) } }) }
-