Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Accessing delegate component of the MapItemView model
Forum Updated to NodeBB v4.3 + New Features

Accessing delegate component of the MapItemView model

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 361 Views
  • 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.
  • V Offline
    V Offline
    Vijaykarthikeyan
    wrote on last edited by Vijaykarthikeyan
    #1

    Im trying to modify the delegate item of the MapItem view model. On Clicked on that item, it's source should be changed while the other items in that view remains same..On clicking the other item,previously changed item should comeback to normal like other items..while currently clicked item should be modified. I succesfully implemented the logic.But,upon running,when i click the 1st item,it is changing the last item and so on.TFor example,when i click the 1st item,on debugging it shows correctly the first item is 1.But,on map,it is modifying the last.the index are reversed.how to solve this.

    this is my code snippet:

    
            MapItemView
                {
                    id:markerItem2
                    model: routeQuery2.waypoints
    
                    delegate: MapQuickItem
                                    {
                                        id:mark2
                                        anchorPoint.x: waypointMarker.width / 2
                                        anchorPoint.y: waypointMarker.height / 2
                                        coordinate: modelData
                                        visible: true
    
                                            sourceItem: Image
                                                            {
                                                                id: waypointMarker
                                                                source: "qrc:/resources/images/location.png"
                                                                width: 32
                                                                height: 32
                                                                fillMode: Image.PreserveAspectFit
                                                                z:2
                                                                visible:true
                                                                transformOrigin: Item.Center
    
                                                                MouseArea
                                                                    {
                                                                        anchors.fill: parent
    
                                                                        onClicked :function(mouse)
                                                                                        {
                                                                                            mouse.accepted=true
                                                                                            // Access properties of the clicked item using index
                                                                                            //var clickedItemData = markerItem2.model[index];
    
    
                                                                            // Access properties of the clicked item using index
                                                                            var clickedIndex = markerItem2.children[index];
    
                                                                            for (var marker in markerItem2.children)
                                                                                        {
                                                                                           if(markerItem2.model[marker]===markerItem2.model[index])
                                                                                           {
                                                                                               console.log(marker)
                                                                                                markerItem2.children[marker].sourceItem.source = "qrc:/resources/images/locate_green.png";
                                                                                            }else
                                                                                               markerItem2.children[marker].sourceItem.source= "qrc:/resources/images/location.png";
                                                                                        }
    
                                                                        }
                                                                    }
                                                            }
                                    }
    
                }
    
    GrecKoG 1 Reply Last reply
    0
    • GrecKoG GrecKo

      @Vijaykarthikeyan Don't modify delegates directly.

      Add a property outside of them and modify that property. Here you can do it with a selectedWaypoint property in the MapItemView, the code is then much simpler:

       MapItemView {
          id: markerItem2
          model: routeQuery2.waypoints
          property int selectedWaypoint: -1
          
          delegate: MapQuickItem {
              id: mark2
              anchorPoint.x: waypointMarker.width / 2
              anchorPoint.y: waypointMarker.height / 2
              coordinate: modelData
              
              sourceItem: Image {
                  readonly property bool isSelected: index === markerItem2.selectedWaypoint
                  source: "qrc:/resources/images/" + (isSelected ? "locate_green.png" : "location.png")
                  width: 32
                  height: 32
                  fillMode: Image.PreserveAspectFit
                  z:2
                  visible:true
                  transformOrigin: Item.Center
                  TapHandler {
                      onTapped: markerItem2.selectedWaypoint = index
                  }
              }
          }    
      }
      
      V Offline
      V Offline
      Vijaykarthikeyan
      wrote on last edited by
      #3

      @GrecKo Thank you.. you have solved my greatest problem...I'm seraching the solution for the past 1 week..thank you once again..thank you so much

      1 Reply Last reply
      0
      • V Vijaykarthikeyan

        Im trying to modify the delegate item of the MapItem view model. On Clicked on that item, it's source should be changed while the other items in that view remains same..On clicking the other item,previously changed item should comeback to normal like other items..while currently clicked item should be modified. I succesfully implemented the logic.But,upon running,when i click the 1st item,it is changing the last item and so on.TFor example,when i click the 1st item,on debugging it shows correctly the first item is 1.But,on map,it is modifying the last.the index are reversed.how to solve this.

        this is my code snippet:

        
                MapItemView
                    {
                        id:markerItem2
                        model: routeQuery2.waypoints
        
                        delegate: MapQuickItem
                                        {
                                            id:mark2
                                            anchorPoint.x: waypointMarker.width / 2
                                            anchorPoint.y: waypointMarker.height / 2
                                            coordinate: modelData
                                            visible: true
        
                                                sourceItem: Image
                                                                {
                                                                    id: waypointMarker
                                                                    source: "qrc:/resources/images/location.png"
                                                                    width: 32
                                                                    height: 32
                                                                    fillMode: Image.PreserveAspectFit
                                                                    z:2
                                                                    visible:true
                                                                    transformOrigin: Item.Center
        
                                                                    MouseArea
                                                                        {
                                                                            anchors.fill: parent
        
                                                                            onClicked :function(mouse)
                                                                                            {
                                                                                                mouse.accepted=true
                                                                                                // Access properties of the clicked item using index
                                                                                                //var clickedItemData = markerItem2.model[index];
        
        
                                                                                // Access properties of the clicked item using index
                                                                                var clickedIndex = markerItem2.children[index];
        
                                                                                for (var marker in markerItem2.children)
                                                                                            {
                                                                                               if(markerItem2.model[marker]===markerItem2.model[index])
                                                                                               {
                                                                                                   console.log(marker)
                                                                                                    markerItem2.children[marker].sourceItem.source = "qrc:/resources/images/locate_green.png";
                                                                                                }else
                                                                                                   markerItem2.children[marker].sourceItem.source= "qrc:/resources/images/location.png";
                                                                                            }
        
                                                                            }
                                                                        }
                                                                }
                                        }
        
                    }
        
        GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #2

        @Vijaykarthikeyan Don't modify delegates directly.

        Add a property outside of them and modify that property. Here you can do it with a selectedWaypoint property in the MapItemView, the code is then much simpler:

         MapItemView {
            id: markerItem2
            model: routeQuery2.waypoints
            property int selectedWaypoint: -1
            
            delegate: MapQuickItem {
                id: mark2
                anchorPoint.x: waypointMarker.width / 2
                anchorPoint.y: waypointMarker.height / 2
                coordinate: modelData
                
                sourceItem: Image {
                    readonly property bool isSelected: index === markerItem2.selectedWaypoint
                    source: "qrc:/resources/images/" + (isSelected ? "locate_green.png" : "location.png")
                    width: 32
                    height: 32
                    fillMode: Image.PreserveAspectFit
                    z:2
                    visible:true
                    transformOrigin: Item.Center
                    TapHandler {
                        onTapped: markerItem2.selectedWaypoint = index
                    }
                }
            }    
        }
        
        V 1 Reply Last reply
        2
        • GrecKoG GrecKo

          @Vijaykarthikeyan Don't modify delegates directly.

          Add a property outside of them and modify that property. Here you can do it with a selectedWaypoint property in the MapItemView, the code is then much simpler:

           MapItemView {
              id: markerItem2
              model: routeQuery2.waypoints
              property int selectedWaypoint: -1
              
              delegate: MapQuickItem {
                  id: mark2
                  anchorPoint.x: waypointMarker.width / 2
                  anchorPoint.y: waypointMarker.height / 2
                  coordinate: modelData
                  
                  sourceItem: Image {
                      readonly property bool isSelected: index === markerItem2.selectedWaypoint
                      source: "qrc:/resources/images/" + (isSelected ? "locate_green.png" : "location.png")
                      width: 32
                      height: 32
                      fillMode: Image.PreserveAspectFit
                      z:2
                      visible:true
                      transformOrigin: Item.Center
                      TapHandler {
                          onTapped: markerItem2.selectedWaypoint = index
                      }
                  }
              }    
          }
          
          V Offline
          V Offline
          Vijaykarthikeyan
          wrote on last edited by
          #3

          @GrecKo Thank you.. you have solved my greatest problem...I'm seraching the solution for the past 1 week..thank you once again..thank you so much

          1 Reply Last reply
          0
          • V Vijaykarthikeyan has marked this topic as solved on

          • Login

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