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
Forum Updated to NodeBB v4.3 + New Features

QML Variant property undefined

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 808 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 17 Aug 2022, 11:06 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
    
    O 1 Reply Last reply 17 Aug 2022, 16:00
    0
    • D dalishi
      17 Aug 2022, 11:06

      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
      
      O Offline
      O Offline
      ODБOï
      wrote on 17 Aug 2022, 16:00 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 17 Aug 2022, 17:27 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.

        O 1 Reply Last reply 17 Aug 2022, 19:24
        0
        • D dalishi
          17 Aug 2022, 17:27

          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.

          O Offline
          O Offline
          ODБOï
          wrote on 17 Aug 2022, 19:24 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 18 Aug 2022, 02:10 last edited by
            #5

            @ODБOï thanks.

            O 1 Reply Last reply 21 Aug 2022, 22:41
            0
            • D dalishi
              18 Aug 2022, 02:10

              @ODБOï thanks.

              O Offline
              O Offline
              ODБOï
              wrote on 21 Aug 2022, 22:41 last edited by
              #6

              @dalishi you're welcome 😉

              1 Reply Last reply
              0

              1/6

              17 Aug 2022, 11:06

              • Login

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