Not able to assign value of array element to property of QML module
-
Dear developers,
I have initialized values of an array "schaltSchranks" in a QML module "map" and trying access its value in another module "mainwindow.qml". but could not able to do it. I would be grateful to you if you please inform me what correction should I do in my code.
/// Module in which values in array is pushed Map { id: map property var schaltSchranks property int newSchaltSchrankID; function addSchaltSchrank() { var count = map.schaltSchranks.length newSchaltSchrankID = getNewSchaltSchrankID(); var schaltSchrank = Qt.createQmlObject ('SchaltSchrank{}', map) map.addMapItem(schaltSchrank) schaltSchrank.z = map.z+1 schaltSchrank.coordinate = mouseArea.lastCoordinate //update list of SS var myArrayUseToArrangeElementOfSSarray = [] if(newSchaltSchrankID <= count) // If new ID assigned for new SS is between lowest and highest ID { for (var i = 0; i<count; i++){ if(i===(newSchaltSchrankID-1)) { myArrayUseToArrangeElementOfSSarray.push(schaltSchrank) // then push new SS at right location in SS array according to its ID } else { myArrayUseToArrangeElementOfSSarray.push(schaltSchranks[i]) } } } else // otherwise push new SS at the end of SS array { for (var j = 0; j<count; j++){ myArrayUseToArrangeElementOfSSarray.push(schaltSchranks[j]) } myArrayUseToArrangeElementOfSSarray.push(schaltSchrank) } schaltSchranks =myArrayUseToArrangeElementOfSSarray } } /// Each element of schaltSchrank is: MapQuickItem { id: schaltSchrank property alias lastMouseX: schaltSchrankMouseArea.lastX property alias lastMouseY: schaltSchrankMouseArea.lastY property alias schaltSchrankColor: image.color property int schaltSchrankID: 0 property int schaltSchrankTelefonNummer: 0 property string standort: "a" property int breite: 0 property int lange: 0 property bool hauptschrank: false property int hauptschrankNummer: 0 } /// Main module where I am trying to access values of array "schaltSchranks" import "map" SchaltschrankPropertyDialogBox { id: schaltSchrankPropertyDialog schlatschrankIDvalue: map.schaltSchranks[map.currentSchaltSchrank].schaltSchrankID onOKButtonClicked: { console.log("Schaltschrank ID: " + map.schaltSchranks[map.currentSchaltSchrank].schaltSchrankID) // Here I am to access schaltSchranks without problem } } ///Error I am getting: Error: qrc:/MainWindow.qml:134: TypeError: Cannot read property 'schaltSchrankID' of undefined
-
@saurabh162 said in Not able to assign value of array element to property of QML module:
TypeError: Cannot read property 'schaltSchrankID' of undefined
Have a look here:
-
hello @Pl45m4
Thank you very much for fast reply !! I have read link mentioned by you but I could not understand how can I use mentioned solution in my cas. Can you please elaborate little bit for me. Sorry I am new to QML. Thank you very much !!
-
I'm not an expert when it comes to QML, but
map
is yourid
ofMap
module. If what the guy on SO said, is correct, you can't access your "Schaltschränke" from another module by their id.This line
schlatschrankIDvalue: map.schaltSchranks[map.currentSchaltSchrank].schaltSchrankID
-
@Pl45m4 thank you very much for update. But I am able to do so in next statement in same module " SchaltschrankPropertyDialogBox", where I am printing its value using console.log . I have also updated console.log statement in "SchaltschrankPropertyDialogBox"" module in my post given above for your kind consideration. Thanks !!