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 Variant property undefined

QML Variant property undefined

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 1.0k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    dalishi
    wrote on last edited by
    #1

    Hi i have a basic QML question might be stupid. I saw in this QML map viewer example, markers is defined as a variant property.

    Map {
        property variant markers
    

    and later directly use like an array

    var count = map.markers.length
    

    How the QML knows this is an array? I must have done something wrong, I actually got the following error:

    TypeError: Cannot read property 'length' of undefined
    
    ODБOïO 1 Reply Last reply
    0
    • D dalishi

      Hi i have a basic QML question might be stupid. I saw in this QML map viewer example, markers is defined as a variant property.

      Map {
          property variant markers
      

      and later directly use like an array

      var count = map.markers.length
      

      How the QML knows this is an array? I must have done something wrong, I actually got the following error:

      TypeError: Cannot read property 'length' of undefined
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @dalishi hello
      Can you please show a how are you trying to access that property in your QML file ?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dalishi
        wrote on last edited by
        #3

        Hi @ODБOï just like this

        var count = map.markers.length
        

        I guess QML does not know it is an array, and I found two ways to solve it

        Map {
            property variant markers: []
        

        or

        Component.onCompleted: {
                markers = new Array(0);
            }
        

        i'm not sure if this is the right way to do it. I'm still learning QML.

        ODБOïO 1 Reply Last reply
        0
        • D dalishi

          Hi @ODБOï just like this

          var count = map.markers.length
          

          I guess QML does not know it is an array, and I found two ways to solve it

          Map {
              property variant markers: []
          

          or

          Component.onCompleted: {
                  markers = new Array(0);
              }
          

          i'm not sure if this is the right way to do it. I'm still learning QML.

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #4

          @dalishi as you can read here the variant type is obsolete now, you should use var and declare it similar to what you did

           property var markers: []
          
          1 Reply Last reply
          1
          • D Offline
            D Offline
            dalishi
            wrote on last edited by
            #5

            @ODБOï thanks.

            ODБOïO 1 Reply Last reply
            0
            • D dalishi

              @ODБOï thanks.

              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by
              #6

              @dalishi you're welcome 😉

              1 Reply Last reply
              0

              • Login

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