C++ 向Qt QML地图添加标记/位置?
你好!我正在设计一个基于Qt的GUI平台,它显示地图,并允许用户在地图顶部添加标记/插针C++ 向Qt QML地图添加标记/位置?,c++,qt,qml,qt5,C++,Qt,Qml,Qt5,你好!我正在设计一个基于Qt的GUI平台,它显示地图,并允许用户在地图顶部添加标记/插针 Plugin { id: mapPlugin name: "osm" } 我正在使用以下QML在QtQuickWidget中渲染地图: Plugin { id: mapPlugin name: "osm" } 我想允许用户使用表单上的按钮在地图上添加交互式pin。用户可以按住地图上的一个点,打开表单,在这里用户可以命名地点,然后按OK Plugin { id: m
Plugin {
id: mapPlugin
name: "osm"
}
我正在使用以下QML在QtQuickWidget中渲染地图:
Plugin {
id: mapPlugin
name: "osm"
}
我想允许用户使用表单上的按钮在地图上添加交互式pin。用户可以按住地图上的一个点,打开表单,在这里用户可以命名地点,然后按OK
Plugin {
id: mapPlugin
name: "osm"
}
Plugin {
id: mapPlugin
name: "osm"
}
我想要实现的示例:
Plugin {
id: mapPlugin
name: "osm"
}
Plugin {
id: mapPlugin
name: "osm"
}
Plugin {
id: mapPlugin
name: "osm"
}
项目结构:
Plugin {
id: mapPlugin
name: "osm"
}
有人能告诉我如何允许用户按住地图上的左键,然后在该点添加标记吗?一个可能的解决方案是使用
MapItemView
并创建一个存储坐标的模型:
Plugin {
id: mapPlugin
name: "osm"
}
markermodel.h
Plugin {
id: mapPlugin
name: "osm"
}
#ifndef MARKERMODEL_H
#define MARKERMODEL_H
#include <QAbstractListModel>
#include <QGeoCoordinate>
class MarkerModel : public QAbstractListModel
{
Q_OBJECT
public:
using QAbstractListModel::QAbstractListModel;
enum MarkerRoles{positionRole = Qt::UserRole + 1};
Q_INVOKABLE void addMarker(const QGeoCoordinate &coordinate){
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_coordinates.append(coordinate);
endInsertRows();
}
int rowCount(const QModelIndex &parent = QModelIndex()) const override{
Q_UNUSED(parent)
return m_coordinates.count();
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override{
if (index.row() < 0 || index.row() >= m_coordinates.count())
return QVariant();
if(role== MarkerModel::positionRole)
return QVariant::fromValue(m_coordinates[index.row()]);
return QVariant();
}
QHash<int, QByteArray> roleNames() const{
QHash<int, QByteArray> roles;
roles[positionRole] = "position";
return roles;
}
private:
QList<QGeoCoordinate> m_coordinates;
};
#endif // MARKERMODEL_H
main.cpp
Plugin {
id: mapPlugin
name: "osm"
}
#include "markermodel.h"
#include <QApplication>
#include <QQuickWidget>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QQuickWidget w;
MarkerModel model;
w.rootContext()->setContextProperty("markerModel", &model);
w.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
w.show();
return a.exec();
}
#包括“markermodel.h”
#包括
#包括
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
qw;
MarkerModel模型;
w、 rootContext()->setContextProperty(“markerModel”和&model);
w、 setSource(QUrl(QStringLiteral(“qrc:/main.qml”));
w、 show();
返回a.exec();
}
Plugin {
id: mapPlugin
name: "osm"
}
完整的示例可以在下面找到。我的意思是,创建包含大量对象(标记)的QML文件没有意义正在添加到同一个文件中。我尝试将MapCircle项添加到我的Map.qml中,但即使这样也不可见。添加了项目结构和Map.qmlow!非常令人印象深刻。这似乎是解决方案。我正在使用QQuickWidget,它使用Qt设计选项卡,而未在main.cpp中定义。您能建议我如何设置w.rootCont吗ext()->setContextProperty(“markerModel”,&model);对于我使用GUI设计器实现的QQuickWidget,我尝试使用ui->edge\u map->rootContext()->setContextProperty(“markerModel”,&model)引用它;但它不工作…/mainwindow.cpp:32:32:错误:无效使用不完整的类型“class QQmlContext”ui->edge_map->rootContext()->setContextProperty(“markerModel”,model);^我现在正在处理它,将完成它,然后接受答案:)好的,地图现在有一个空的标记模型覆盖,但我不明白你是如何定义标记(数据类型)的?这是一个非常复杂的答案,感谢您的努力。这种方法不是很有用,因为标记会被擦除,并且不会存储到硬盘中。此外,单击标记无法激活标记。
Plugin {
id: mapPlugin
name: "osm"
}