在代表中介绍条件

问题描述 投票:0回答:2

我寻求在代表中引入一个条件。

这是一个简化的main.qml

import QtQuick 2.6
import QtQuick.Window 2.2
import QtPositioning 5.5
import QtLocation 5.6

Window {
    width: 1440
    height: 900
    visible: true

    property variant topLeftEurope: QtPositioning.coordinate(60.5, 0.0)
    property variant bottomRightEurope: QtPositioning.coordinate(51.0, 14.0)
    property variant boundingBox: QtPositioning.rectangle(topLeftEurope, bottomRightEurope)

    Map {
        id: mainMap
        anchors.centerIn: parent;
        anchors.fill: parent
        plugin: Plugin {name: "osm"}

        MapItemView {

            model: myModel

            delegate: Marker{}   

        }
        visibleRegion: boundingBox
    }
}

它显示地图并定义边界框。

这是代表:Marker.qml

import QtQuick 2.4
import QtLocation 5.6



MapQuickItem {
    id: mark

coordinate: position //"position" is roleName

    ... all the stuff for the marker to be displayed on the map
}

我希望添加此条件以丢弃不在要显示的边界框内的点:

if (main.boundingBox.contains(position)){
    ... display the marker on the map
}

但如果不能直接在我的代码中使用。

我试过添加一个函数:

function isMarkerViewable(){
    if (!main.boundingBox.contains(position))
        return;
}

但我也无法打电话给它。

是否可以在委托中添加条件,如果是,则如何执行此操作?

谢谢你的帮助

qt delegates qml qtlocation
2个回答
1
投票

正如@derM评论一个选项是使用Loader,在下面的例子中,每个点都有一个名为type的属性,用于区分哪些项应该绘制为矩形或圆形。

Marker.qml

import QtQuick 2.0
import QtLocation 5.6

MapQuickItem {
    sourceItem: Loader{
        sourceComponent:
            if(type == 0)//some condition
                return idRect
            else if(type == 1) //another condition
                return idCircle

    }
    Component{
        id: idRect
        Rectangle{
            width: 20
            height: 20
            color: "blue"
        }
    }
    Component{
        id: idCircle
        Rectangle{
            color: "red"
            width: 20
            height: 20
            radius: 50
        }
    }
}

main.qml

MapItemView {
    model: navaidsModel
    delegate:  Marker{
        coordinate:  position
    }
}

输出:

enter image description here

您可以在以下link中找到完整的示例。


0
投票

如果您的目标与性能优化无关(不加载不需要的项目),但它只与您的业务逻辑相关,那么对我来说最简单的解决方案似乎是使用MapQuickItem或源组件的visible属性。喜欢:

visible: main.boundingBox.contains(position)
© www.soinside.com 2019 - 2024. All rights reserved.