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. [QML] List all variables like in a loop or something else

[QML] List all variables like in a loop or something else

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 7.6k 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.
  • F Offline
    F Offline
    freaksdd
    wrote on 31 Aug 2016, 07:57 last edited by
    #1

    hi, is there a way in QML to list all variables I've created in an Item like in a loop or something else? I have a Source File, where i have only variables and i have to have there in a Database, but i don't want to write variable by variable.

    example:

    Item {
    
    property double number1: 1.2
    property var newDate = new Date()
    property int number2 = 6
    }
    

    Thanks for help

    R 1 Reply Last reply 31 Aug 2016, 09:05
    0
    • F freaksdd
      31 Aug 2016, 07:57

      hi, is there a way in QML to list all variables I've created in an Item like in a loop or something else? I have a Source File, where i have only variables and i have to have there in a Database, but i don't want to write variable by variable.

      example:

      Item {
      
      property double number1: 1.2
      property var newDate = new Date()
      property int number2 = 6
      }
      

      Thanks for help

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 31 Aug 2016, 09:05 last edited by
      #2

      @freaksdd
      set the objectName property of your Items you want to save (can even be the same for all).
      Then get the root object (via the engine). And then find all children and use their metaObject() to traverse the properties.

      QML:

      Item {
          objectName: "item_to_save"
          ....
      }
      

      C++:

      QQuickView view;
      QObject* rootItem = view.rootObject();
      if ( QObject* item= rootItem->findChildren<QObject*>("item_to_save") )
      {
          const QMetaObject * mo = item->metaObject();
          for( int i = mo->propertyOffset(); i < mo->propertyCount(); ++i )
          {
                QMetaProperty prop = mo->property(i) ;
                QString name = QString::fromLatin1(prop.name());
                QVariant value = prop.read(item);
               
                ...
          }
      }
      

      Should do it, but haven't tested it.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • P Offline
        P Offline
        p3c0
        Moderators
        wrote on 31 Aug 2016, 10:03 last edited by
        #3

        @freaksdd

        A pure QML way would be as follows:

        Item {
            id: item
            property double number1: 1.2
            property var newDate : new Date()
            property int number2 : 6
        
            Component.onCompleted: {
                for(var property in item)
                    console.log(property)
            }
        }
        

        But note that this lists all the properties including the properties inherited by that Item. So in case if you want to filter your properties you can add some unique prefix to your properties and check. Something like:

        if(property.substring(0, 4)==="str_") ...
        

        157

        1 Reply Last reply
        0

        1/3

        31 Aug 2016, 07:57

        • Login

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