Results 1 to 7 of 7

Thread: TypeError: Cannot read property 'length' of undefined in QML array

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default TypeError: Cannot read property 'length' of undefined in QML array

    Alias.qml
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle {
    4. width: 100
    5. height: 62
    6.  
    7. property var arr : [1,2,3,4]
    8. }
    To copy to clipboard, switch view to plain text mode 

    Access.qml

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle {
    4. id: newq
    5.  
    6. width: 100
    7. height: 62
    8.  
    9. property var yy : [1]
    10. property var pp : [1]
    11.  
    12. onPpChanged:
    13. {
    14. console.log("\npp: " + pp.pop())
    15. console.log("\nyy: " + newq.yy.length + "\n")
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    main.qml
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle {
    4. id: root
    5. width: 360
    6. height: 360
    7.  
    8. Alias
    9. {
    10. id: poi
    11. }
    12.  
    13. Access
    14. {
    15. pp: poi.arr
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    The error shows up on this line:
    Qt Code:
    1. console.log("\nyy: " + newq.yy.length + "\n")
    To copy to clipboard, switch view to plain text mode 

    Where am I going wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TypeError: Cannot read property 'length' of undefined in QML array

    Do you also get that when you run just Access.qml?
    Have you tried without "newq"? You don't use that for access to "pp".

    Cheers,
    _

  3. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: TypeError: Cannot read property 'length' of undefined in QML array

    Quote Originally Posted by anda_skoa View Post
    Do you also get that when you run just Access.qml?
    The error appears only when I access and thus assign the property of Access.qml in main.qml.

    Quote Originally Posted by anda_skoa View Post
    Have you tried without "newq"? You don't use that for access to "pp".
    A drowning person grabs a straw. I tried newq because the original way wasn't working - I know it is not needed.

    Would you please try the code on your machine (if you have it nearby)?

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TypeError: Cannot read property 'length' of undefined in QML array

    The yy property is just not set yet when pp gets changed.

    If you add a onYyChanged handler you'll see that it gets set after the other one.

    So it is still undefined when the first change handler is invoked.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    TheIndependentAquarius (12th June 2014)

  6. #5
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: TypeError: Cannot read property 'length' of undefined in QML array

    thankful to you, got the point now.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TypeError: Cannot read property 'length' of undefined in QML array

    In situations such as this it is wise to use a Component.onCompleted handler to make sure the other method only gets executed when the object is fully constructed:

    javascript Code:
    1. Item {
    2. property var prop
    3. property var prop2
    4.  
    5. property bool __constructed: false
    6.  
    7. onPropChanged: {
    8. if(!__constructed) return
    9. doSomethingWith(prop2)
    10. }
    11. Component.onCompleted: {
    12. __constructed = true
    13. // do some other initial stuff
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    TheIndependentAquarius (12th June 2014)

  9. #7
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: TypeError: Cannot read property 'length' of undefined in QML array

    wysota

    That was very helpful - much thankful to you too.

Similar Threads

  1. Replies: 1
    Last Post: 30th December 2013, 14:53
  2. read sqlite to array
    By vanduongbk in forum Newbie
    Replies: 3
    Last Post: 1st October 2013, 14:08
  3. Replies: 1
    Last Post: 1st July 2013, 14:27
  4. Replies: 0
    Last Post: 28th May 2012, 19:56
  5. read from file into array
    By obad in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2010, 08:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.