首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在委托中引入条件

在委托中引入条件
EN

Stack Overflow用户
提问于 2017-12-20 14:50:36
回答 2查看 506关注 0票数 0

我想在一位代表中介绍一个条件。

下面是一个简化的main.qml

代码语言:javascript
复制
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

代码语言:javascript
复制
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
}

我想添加这个条件,以丢弃那些不在要显示的边界框内的点:

代码语言:javascript
复制
if (main.boundingBox.contains(position)){
    ... display the marker on the map
}

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

我尝试添加一个函数:

代码语言:javascript
复制
function isMarkerViewable(){
    if (!main.boundingBox.contains(position))
        return;
}

但我也不能这么说。

是否可以在委托中添加条件,如果可以,如何实现?

谢谢你的帮忙

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-20 20:23:13

@derM注释所示,一个选项是使用Loader,在下面的示例中,每个点都有一个名为type的属性,用于区分哪些项应该绘制为矩形还是圆形。

Marker.qml

代码语言:javascript
复制
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

代码语言:javascript
复制
MapItemView {
    model: navaidsModel
    delegate:  Marker{
        coordinate:  position
    }
}

输出:

您可以在下面的链接中找到一个完整的示例。

票数 1
EN

Stack Overflow用户

发布于 2017-12-21 16:13:54

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

代码语言:javascript
复制
visible: main.boundingBox.contains(position)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47908654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档