c++ - 可以在QML中的 map 上绘制带有孔的MapPolygon吗?

标签 c++ qt qml qtpositioning

在 C++ 中,我可以使用 QPainterPath::subtracted 绘制带孔的多边形,但我不知道在 QML Map 中如何实现。

最佳答案

Qt 5.13可以使用Mapbox GL Plugin ,例如使用 QGeoPolygon ,您还可以使用 GeoJSON。例如:

ma​​in.cpp

#include <QGeoPolygon>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QGeoPolygon polygon;
    polygon.setPath({{51.11, 17.13},
                     {50.42, 30.54},
                     {58.36, 26.70},
                     {51.11, 17.13}});
    polygon.addHole({{54.36, 23.46},
                     {51.91, 20.52},
                     {51.50, 28.25},
                     {54.36, 26.80},
                     {54.36, 23.46}});

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("poly", QVariant::fromValue(polygon));
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

ma​​in.qml

import QtQuick 2.12
import QtQuick.Window 2.12
import QtLocation 5.13
import QtPositioning 5.13

Window {
    visible: true
    width: 640
    height: 480
    Map {
        anchors.fill: parent
        center: QtPositioning.coordinate(52, 22)
        plugin: Plugin {
            name: "mapboxgl"
        }
        zoomLevel: 4
        MapPolygon {
            color: "red"
            border.color: "green"
            border.width: 2
            smooth: true
            opacity: 0.25
            geoShape: poly
        }
    }
}

enter image description here

关于c++ - 可以在QML中的 map 上绘制带有孔的MapPolygon吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57054554/

相关文章:

c++ - Qt-5.14.0:QML下的Vulkan导致std::system_error::互斥锁失败

c++ - 适合中级程序员成为专家程序员的好 C++ 书籍?

c++ - 计算循环中的对象

linux - 在 QProcess 输出中保留 ANSI 转义序列

c++ - 什么是 QVariant,什么时候应该使用它?

c++ - 如何通过 Q_PROPERTY 将指向 Q_GADGET 的指针暴露给 QML

html - 如何用QT解析HTML文件?

c++ - 如何使用模板类作为模板参数?

c++ - 处理图像的多重转换

android - QML导入错误