You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt 6.7.1:MapPolyline置于MapView下不显示的问题咨询

MapPolyline在MapView下无法显示的解决方法

我正在尝试将MapPolyline添加到MapView组件中(使用Qt 6.7.1版本),当MapPolyline作为Map的子组件时折线能正常显示,但改为MapView作为父组件后折线就消失了。Qt官方文档里也没有MapPolyline和MapView配合的示例,请问需要修改什么才能让折线正常显示?

演示问题的原始代码:

import QtQuick
import QtPositioning
import QtLocation

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Map View")

    // MapPolyline works when used inside Map component
    Map {
        id: mapView
        anchors.fill: parent
        center: QtPositioning.coordinate(-27.5, 153.0)
        plugin: Plugin {
            id: osmPlugin
            name: "osm"
        }

        MapPolyline {
            line.width: 3
            line.color: 'green'
            path: [
                { latitude: -27, longitude: 153.0 },
                { latitude: -27, longitude: 154.1 },
                { latitude: -28, longitude: 153.5 },
                { latitude: -29, longitude: 153.5 }
            ]
        }
    }

    /////////   Changing the Map to MapView causes the line to disappear //////
    // MapView {
    //     id: mapView
    //     anchors.fill: parent

    //     map {
    //         center: QtPositioning.coordinate(-27.5, 153.0)
    //         zoomLevel: 12
    //         plugin: Plugin {
    //             id: osmPlugin
    //             name: "osm"
    //         }
    //     }

    //     MapPolyline {
    //         line.width: 3
    //         line.color: 'green'
    //         path: [
    //             { latitude: -27, longitude: 153.0 },
    //             { latitude: -27, longitude: 154.1 },
    //             { latitude: -28, longitude: 153.5 },
    //             { latitude: -29, longitude: 153.5 }
    //         ]
    //     }
    // }
}

解决方法

MapView是封装了Map的容器组件,所有地图图层元素(包括MapPolyline、MapMarker等)必须挂载到MapView内部的map对象下,而不是直接作为MapView的子组件。

修改后的完整代码:

import QtQuick
import QtPositioning
import QtLocation

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Map View")

    MapView {
        id: mapView
        anchors.fill: parent

        map {
            center: QtPositioning.coordinate(-27.5, 153.0)
            zoomLevel: 12
            plugin: Plugin {
                id: osmPlugin
                name: "osm"
            }

            // 将MapPolyline移动到map块内部
            MapPolyline {
                line.width: 3
                line.color: 'green'
                path: [
                    { latitude: -27, longitude: 153.0 },
                    { latitude: -27, longitude: 154.1 },
                    { latitude: -28, longitude: 153.5 },
                    { latitude: -29, longitude: 153.5 }
                ]
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者DailyLearner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 17:20:07