PDA

View Full Version : TypeError: Cannot read property ‘source’ of undefined



stereoMatching
29th June 2013, 23:39
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

Rectangle{

}
}
}

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

stereoMatching
1st July 2013, 14:27
if(layoutLoader.status == Loader.Ready && filesModel.count != 0){
return filesModel.get(layoutLoader.item.currentRow).sourc e
//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