带有多个委托或视图的 QML 单一模型

QML single model attached with multiple delegates or views

我有一个源自 AbstractListModel 的模型 (markerModel),它具有三个角色 statuspositionlabel。我通过在地图上画圆圈来展示它们。同时我想在矩形 rectangle1 上打印他们的 positionlabel。但是 MapItemView 已经有一个代表。一个模型可以有多个代表吗?

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"
}

模型和委托之间没有直接联系,连接两者的是视图。

您可以有尽可能多的视图使用相同的数据源模型,并且您可以在每个视图中拥有您想要的任何不同的委托:

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