0

I have a model (markerModel) derived from AbstractListModel which have three roles status, position and label. I am showing them by drawing circles on map. At the same time I want to print their position and label n a rectangle rectangle1. But MapItemView already have a delegate. Can there be multiple delegates with one model ?

Map {
    id: map
    anchors.fill: parent
    plugin: mapPlugin
    center: QtPositioning.coordinate(22.5726, 88.3639)
    zoomLevel: 14

    MapItemView {
        model: markerModel
        delegate: markerDelegate
    }

    Component {
        id: markerDelegate

        MapQuickItem{
            anchorPoint: Qt.point(2.5, 2.5)
            coordinate: QtPositioning.coordinate(position.x, position.y)
            zoomLevel: 0
            sourceItem: Rectangle{
                width:  settings.marker_size;
                height: settings.marker_size;
                radius: settings.marker_size/2;
                color:  settings.marker_colors[status]
                border.color: "white"
                border.width: 1
            }
        }
    }
}

Rectangle {
    id: rectangle1
    anchors.top: map.top
    anchors.right: map.right
    width: 500
    height: 750
    color: "#ffffff"
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Neel Basu
  • 12,638
  • 12
  • 82
  • 146

1 Answers1

0

There is no direct connection between a model and a delegate, it is the view that connects the two.

You can have as many views using the same model for data source, and you can have whatever different delegates you want in each of those views:

  ListModel {
    id: mod
    ListElement { value: "red" }
    ListElement { value: "green" }
    ListElement { value: "blue" }
    ListElement { value: "cyan" }
    ListElement { value: "magenta" }
  }

  Row {
    ListView {
      width: 100
      height: 250
      model: mod
      delegate: Rectangle {
        width: 100
        height: 50
        color: value
      }
    }
    ListView {
      width: 100
      height: 250
      model: mod
      delegate: Rectangle {
        width: 100
        height: 50
        color: "grey"
        Text {
          anchors.centerIn: parent
          text: value
        }
      }
    }
  }
dtech
  • 47,916
  • 17
  • 112
  • 190
  • So its like QT's Document View, Views connect to a model and have delegate that specifies how each element of the model has to be represented ? – Neel Basu Sep 07 '17 at 17:12
  • Yes, absolutely. – dtech Sep 07 '17 at 17:17
  • 1
    How can I put an arbitrary function as delegate ? would `delegate: function(row){}` work ? – Neel Basu Sep 07 '17 at 19:20
  • The delegate can only be a visual item. You can put the function inside the delegate item. Although I don't see why you'd need any functions inside a delegate, that's just for visual representation. – dtech Sep 07 '17 at 19:24
  • in case I have a graphics component on scene which is not a view e.g. MapPolyLine, I want to have a function that updates the points of the polyline in that function – Neel Basu Sep 07 '17 at 19:31
  • Well, you put it inside the delegate then. – dtech Sep 07 '17 at 19:32
  • Probably this comment is not related to the question, However I think my syntax is somewhere wrong, ` MapItemView { model: markerModel delegate: Item{ function add_point(row){ console.log("here") // add row.position to polyline } } }` I am getting error `QDeclarativeGeoMapItemView map item delegate is of unsupported type.` – Neel Basu Sep 07 '17 at 19:35
  • The documentation for that particular view says the delegate must have a `MapItem`-derived item as the root. – dtech Sep 07 '17 at 19:41
  • Hm can you check my actual problem ? on https://stackoverflow.com/questions/46017163/abstractitemmodel-to-qml-route – Neel Basu Sep 07 '17 at 19:46
  • @dtech What if the views are in different QML files? – pooya13 Jul 30 '20 at 03:12
  • @pooya13 it doesn't matter. You can use the same model for any number of views, in different locations, different delegates and whatnot. – dtech Jul 30 '20 at 04:37
  • @dtech Sorry for the noob question, but does that mean the component properties in QML are essentially all global variables? i.e. the symbol `mod` can be used in a view in a different file? – pooya13 Jul 30 '20 at 05:54
  • @pooya13 no, it doesn't work like that, check this answer, the rules are basically the same: https://stackoverflow.com/questions/39356278/qml-what-is-the-id-and-how-does-it-work – dtech Jul 30 '20 at 12:51
  • @dtech Ok thanks for helping me understand the `id` attribute better. I still have my original question though. How do we use the same model for two views in different files? If we instantiate the model in each file, then won't we have two different copies of the model data? – pooya13 Jul 30 '20 at 19:25
  • 1
    Both views can still reference the same one single model object, you can pass a reference to the model as a property for the view object, or you can use an id or a root object property if the model object is situated down from the current branch. QML files are just prototypes until instances of that prototype are created. All QML objects are part of a single tree, so where in that tree you create your object determines the scope of external data the object can access. See this question as well: https://stackoverflow.com/questions/45469881/access-items-in-different-qml-files – dtech Jul 31 '20 at 04:41
  • Thank you for the explanation. That question also has great answers. A version of this should have been on the first page of the QML documentation! – pooya13 Jul 31 '20 at 07:12