我使用c++中的QGeopath和Pathcontroller类绘制mapPolyline。现在,我想清除该mapPolyline。我正在使用Qgeopath类中的clearPath()函数,并在按钮的onclicked上调用了它。
如何从c++ / qt清除路径。我尝试了这段代码

路径 Controller

class PathController: public QObject{
Q_OBJECT
Q_PROPERTY(QGeoPath geopath READ geoPath WRITE setGeoPath NOTIFY geopathChanged)
public:
PathController(QObject *parent = 0) : QObject(parent) {}

QGeoPath geoPath() const {
    return mGeoPath;
}

void setGeoPath(const QGeoPath &geoPath) {
    if(geoPath != mGeoPath) {
        mGeoPath = geoPath;
        emit geopathChanged();
    }
}


Q_INVOKABLE void clearPath(){
     mGeoPath.clearPath();
 }
signals:
void geopathChanged();

private:
QGeoPath mGeoPath;
};

main.cpp
 int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);

  QGeoPath path;
  // path.addCoordinate(QGeoCoordinate(55.006355, 92.860984));
     path.addCoordinate(QGeoCoordinate(55.1, 93.4567));
     path.addCoordinate(QGeoCoordinate(56.1, 92.777));

    PathController controller;
    controller.setGeoPath(path);
   // path.clearPath();


QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("pathController", &controller);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
     }

main.qml
 Window {
visible: true
width: 640
height: 480


    Plugin{
     id: osmMapPlugin
     name: "here"
     PluginParameter { name: "here.app_id"; value: "oBB4FivcP23m2UZQCj8K"
            }
     PluginParameter { name: "here.token"; value: "P-D8XRRGeVt0YphUuOImeA"
      }
      }
Map {
    anchors.fill: parent
    plugin: osmMapPlugin
    center: QtPositioning.coordinate(56.006355, 92.860984)
    zoomLevel: 10

    MapPolyline {
        id: pl
        line.width: 10
        line.color: 'green'
    }

    Button {
        id: button
        x: 78
        y: 117
        text: qsTr("clear")
        onClicked: {
            pathController.clearPath();
        }
    }
}

function loadPath(){
    var lines = []
    for(var i=0; i < pathController.geopath.size(); i++){
        lines[i] = pathController.geopath.coordinateAt(i)
    }
    return lines;
}
Connections{
    target: pathController
    onGeopathChanged: pl.path = loadPath()
}
Component.onCompleted: pl.path = loadPath()
 }

但是,我在 map 上得到了mapPolyline。 mapPolylines不会清除。问题-如何清除 map 折线

最佳答案

似乎您正在使用my previous answer,当前的mGeoPath并不意味着将为其通知QML,要通知QML,您必须发出geopathChanged信号:

Q_INVOKABLE void clearPath(){
    mGeoPath.clearPath();
    emit geopathChanged();
}

关于c++ - 如何在Qt中使用QGeopath清除Mappolyline,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58741944/

10-09 19:44