[Solved]TypeError: Cannot read property 'source' of undefined----Should I omit this error message?



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

    main.qml
    @
    import QtQuick 2.1
    import QtQuick.Controls 1.0
    import QtQuick.Layouts 1.0
    import QtQuick.Dialogs 1.0

    Rectangle {
    width: 100
    height: 62

    ImageView{
        id:imView
    
        anchors.fill: parent
    }
    
    Image{
        anchors.fill: parent
       //this is where the problem come from
        source: imView.getCurrentFile()
    }
    

    }

    @

    ImageView
    @
    import QtQuick 2.0
    import QtQuick.Controls 1.0
    import QtQuick.Layouts 1.0

    Rectangle {
    width: 100
    height: 62

    function appendFiles(files){
        for(var i = 0; i != files.length; ++i){
            filesModel.append({"source": files[i]})
        }
    }
    

    //this is where the problem come from
    function getCurrentFile(){
    if(loader.status == Loader.Ready){
    return filesModel.get(loader.item.currentRow).source
    }

        return "-1"
    }
    
    ListModel{
        id: filesModel
    }
    
    Loader{
        id: loader
    
        sourceComponent: tableViewHeavyComponent
    }
    
    Component{
        id: tableViewHeavyComponent
    
        TableView{
    
        }
    }
    

    }
    @

    Could I safely omit this error message, if not
    how should I deal with it?



  • Are you having a list of images (filenames) and then an image showing the selected image?

    I would perhaps add a signal to the ImageView for when the selected image is ready and then add it to the source of the Image.

    @ ImageView{
    id:imView

        anchors.fill: parent
    
        onReady: imageContainer.source = theImageSource;
    }
    

    Image{
    id: imageContainer
    anchors.fill: parent
    }

    @

    theImageSource would then be a property in the ImageView that exposes the current source for instance. Or you could just use the function.

    This might fix your issue.



  • Thanks, I found another solution

    @
    if(layoutLoader.status == Loader.Ready && filesModel.count != 0){
    return filesModel.get(layoutLoader.item.currentRow).source
    //return filesModel.get(tableView.currentRow).source
    }
    @

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.