是否可以在QML的地图上绘制带有孔的MapPolygon?

时间:2019-07-16 09:52:15

标签: c++ qt qml qtpositioning

在C ++中,我可以使用QPainterPath :: Subtracted绘制带孔的多边形,但是我不知道在QML Map中怎么可能。

1 个答案:

答案 0 :(得分:1)

由于Qt 5.13可以使用Mapbox GL Plugin,例如使用QGeoPolygon,因此也可以使用GeoJSON。例如:

main.cpp

<field name="code">model.open_raw_materials()</field>

main.qml

#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();
}

enter image description here