我想在一位代表中介绍一个条件。
下面是一个简化的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;
}但我也不能这么说。
是否可以在委托中添加条件,如果可以,如何实现?
谢谢你的帮忙
发布于 2017-12-20 20:23:13
如@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
}
}输出:

您可以在下面的链接中找到一个完整的示例。
发布于 2017-12-21 16:13:54
如果您的目标与性能优化无关(不是加载不需要的项),但它只是与您的业务逻辑相关,那么对我来说,最简单的解决方案似乎是使用MapQuickItem或源组件的可见属性。比如:
visible: main.boundingBox.contains(position)https://stackoverflow.com/questions/47908654
复制相似问题