Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Not able to assign value of array element to property of QML module
Forum Updated to NodeBB v4.3 + New Features

Not able to assign value of array element to property of QML module

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 561 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saurabh162
    wrote on last edited by saurabh162
    #1

    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
    
    Pl45m4P 1 Reply Last reply
    0
    • S saurabh162

      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
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @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:

      • https://stackoverflow.com/questions/36736573/qml-cannot-read-property-xxx-of-undefined

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • S Offline
        S Offline
        saurabh162
        wrote on last edited by
        #3

        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 !!

        Pl45m4P 1 Reply Last reply
        0
        • S saurabh162

          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 !!

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @saurabh162

          I'm not an expert when it comes to QML, but map is your id of Map 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
          

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • S Offline
            S Offline
            saurabh162
            wrote on last edited by
            #5

            @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 !!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved