Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Get properties list from Item

Get properties list from Item

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 351 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • BondrusiekB Offline
    BondrusiekB Offline
    Bondrusiek
    wrote on last edited by
    #1

    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
          //
        }
      }
    }
    
    
    1 Reply Last reply
    1
    • BondrusiekB Offline
      BondrusiekB Offline
      Bondrusiek
      wrote on last edited by
      #2

      I found a solution:

          onClicked: {
            Object.keys(rectangle).forEach((key) =>
                                           {
                                             if (rectangle.hasOwnProperty(key) && typeof rectangle[key] !== "function") {
                                                console.log(key + ": " + rectangle[key])
                                             }
                                           })
          }
      
      1 Reply Last reply
      1
      • BondrusiekB Bondrusiek has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved