Results 1 to 2 of 2

Thread: TypeError: Cannot read property ‘source’ of undefined

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default TypeError: Cannot read property ‘source’ of undefined

    Using loader to load the component but always get error(warning?or just a state report?) message

    main.qml
    Qt Code:
    1. import QtQuick 2.1
    2. import QtQuick.Controls 1.0
    3. import QtQuick.Layouts 1.0
    4. import QtQuick.Dialogs 1.0
    5.  
    6. Rectangle {
    7. width: 100
    8. height: 62
    9.  
    10. ImageView{
    11. id:imView
    12.  
    13. anchors.fill: parent
    14. }
    15.  
    16. Image{
    17. anchors.fill: parent
    18. //this is where the problem come from
    19. source: imView.getCurrentFile()
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    ImageView
    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Controls 1.0
    3. import QtQuick.Layouts 1.0
    4.  
    5. Rectangle {
    6. width: 100
    7. height: 62
    8.  
    9. function appendFiles(files){
    10. for(var i = 0; i != files.length; ++i){
    11. filesModel.append({"source": files[i]})
    12. }
    13. }
    14.  
    15. //this is where the problem come from
    16. function getCurrentFile(){
    17. if(loader.status == Loader.Ready){
    18. return filesModel.get(loader.item.currentRow).source
    19. }
    20.  
    21. return "-1"
    22. }
    23.  
    24. ListModel{
    25. id: filesModel
    26. }
    27.  
    28. Loader{
    29. id: loader
    30.  
    31. sourceComponent: tableViewHeavyComponent
    32. }
    33.  
    34. Component{
    35. id: tableViewHeavyComponent
    36.  
    37. Rectangle{
    38.  
    39. }
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 
    Could I safely omit this error message, if not
    how should I deal with it?

  2. #2
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TypeError: Cannot read property ‘source’ of undefined

    Qt Code:
    1. if(layoutLoader.status == Loader.Ready && filesModel.count != 0){
    2. return filesModel.get(layoutLoader.item.currentRow).source
    3. //return filesModel.get(tableView.currentRow).source
    4. }
    To copy to clipboard, switch view to plain text mode 

    Check the number of the ListModel before query the source
    The filesModel can't find property source because I haven't insert
    anything into it

    edit : add one more condition "layoutLoader.item.currentRow != -1", else it would emit error message
    when you do not select any image
    Last edited by stereoMatching; 1st July 2013 at 14:52.

Similar Threads

  1. Replies: 3
    Last Post: 3rd May 2013, 20:27
  2. Technique to read file property details in Windows 7
    By astodolski in forum General Programming
    Replies: 3
    Last Post: 2nd March 2013, 08:37
  3. Replies: 0
    Last Post: 28th May 2012, 19:56
  4. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  5. Exposing a Qt container property to QtScript, READ and WRITE
    By SingleMalt in forum Qt Programming
    Replies: 6
    Last Post: 12th May 2011, 07:31

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
  •  
Qt is a trademark of The Qt Company.