Qt 在委托中介绍条件

Qt 在委托中介绍条件,qt,delegates,qml,qtlocation,Qt,Delegates,Qml,Qtlocation,我寻求在代理中引入一个条件 下面是一个简化的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,

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

下面是一个简化的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
}
但是if不能直接在我的代码中使用

我尝试添加一个函数:

function isMarkerViewable(){
    if (!main.boundingBox.contains(position))
        return;
}
但我也不能称之为

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

感谢您的帮助

作为注释,一个选项是使用加载器,在下面的示例中,每个点都有一个名为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
        }
    }
}
MapItemView {
    model: navaidsModel
    delegate:  Marker{
        coordinate:  position
    }
}
main.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
        }
    }
}
MapItemView {
    model: navaidsModel
    delegate:  Marker{
        coordinate:  position
    }
}
输出:


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

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

visible: main.boundingBox.contains(position)

简单解决方案:将标记的visible属性设置为布尔条件,使其仍然存在但不可见。感谢您的提示,但这是不可能的。我有大量的分数,如果所有的分数都被处理的话,会占用太多的内存。我必须只加载那些有用的。所以我真的需要一个if语句或等效语句来完成这项工作。在这种情况下,你应该让你的标记模型只返回可见点,我想这应该是可能的。Qt/QML中也有过滤模型,如。我不确定到目前为止是否有任何纯QML版本的过滤器模型。但这将是解决问题的最好办法,从模型开始,而不是从视图开始。谢谢。我将朝着这个解决办法前进。然而,仍然可以在委托中设置条件。例如,如果是这样,则显示一个圆,如果是那样,则显示一个矩形?如果要放弃点,则适当的做法是通过QSortProxyModel在模型中进行过滤,如果要说明要查看的项目类型,则必须在代理中执行此操作。你想做什么?感谢@derM和eyllanesc提供这些新答案。Eyllanesc,再一次,你做得很好。你的回答对我这个学习者来说很有启发性。事实并非如此,性能优化对我来说意义重大。我在全球各地有超过50万个积分需要定位,所以如果我只能在记忆中载入其中的几个,这就是我的成就。