gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:43 27 4
gpt4 key购买 nike

我使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com