【问题标题】:QML single model attached with multiple delegates or views附加了多个委托或视图的 QML 单一模型
【发布时间】:2017-09-06 19:44:22
【问题描述】:

我有一个从AbstractListModel 派生的模型(markerModel),它具有三个角色statuspositionlabel。我通过在地图上画圆圈来展示它们。同时我想打印他们的positionlabel n 一个矩形rectangle1。但是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"
}

【问题讨论】:

    标签: qt qml qtquick2


    【解决方案1】:

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

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

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

    【讨论】:

    • 所以它就像 QT 的文档视图,视图连接到模型并具有指定模型的每个元素必须如何表示的委托?
    • 是的,绝对的。
    • 如何将任意函数作为委托? delegate: function(row){} 会工作吗?
    • 代理只能是可视项。您可以将函数放在委托项目中。虽然我不明白为什么在委托中需要任何函数,但这只是为了视觉表示。
    • 两个视图仍然可以引用同一个模型对象,您可以将模型的引用作为视图对象的属性传递,或者如果模型对象可以使用 id 或根对象属性对象位于当前分支的下方。在创建该原型的实例之前,QML 文件只是原型。所有 QML 对象都是单个树的一部分,因此您在该树中创建对象的位置决定了该对象可以访问的外部数据的范围。也请参阅此问题:stackoverflow.com/questions/45469881/…
    猜你喜欢
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多