c++ - 如何在 Qt 中使用 QGeopath 清除 Mappolyline

标签 c++ qt qml

我使用 C++ 中的 QGeopath 和 Pathcontroller 类绘制 mapPolyline。现在我想清除那个 mapPolyline。我正在使用 Qgeopath 类中的 clearPath() 函数,并在单击按钮时调用它。 如何从 c++/qt 清除路径。我试过这段代码

路径 Controller .h

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;
};

主要.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();
     }

主.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 不明确。问题 - 如何清除 Mappolylines

最佳答案

看来你用的是my previous answer ,当前的 mGeoPath 并不意味着 QML 会收到通知,要通知 QML,您必须发出 geopathChanged 信号:

Q_INVOKABLE void clearPath(){
    mGeoPath.clearPath();
    <b>emit geopathChanged();</b>
}

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

相关文章:

qt - 如何让不倒翁到 'base style'

c++ - GNU gdb 无法进入模板函数(OS X Mavericks)

qt - 在 Windows 上 setRange(0, 0) 时将文本放在 QProgressBar 的中间?

c++ - 将 GLFW 连接到 QOpenGLWidget

qml - 第二次点击后图像大小不同

qt - 将 QML StackView 重置为初始状态

c++ - 带有可选参数的 Poco 选项

c++ - c++中的fstream指针映射

c# - 从 C# 调用 C++ DLL

qt - 如何将 mousePressEvent() 传播到一组 QGraphicsItem?