I'm trying to pass variable from c++ to qml. If you think some codes missing I can publish the parts you want in comments. I don't want to make it long or hard to read.
I want to send Coordinate X and Y to qml when I press save button. After that I want to get a marker in that position.
mainwindow.h(just signal part):
signals:
void sendCoord(float x,float y);
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
rsu.h:
#ifndef RSU_H
#define RSU_H
#include <string>
class rsu
{
public:
std::string id;
std::string name;
double coorx;
double coory;
std::string ip;
int port;
};
#endif // RSU_H
parts of mainwindow.cpp:
MarkerModel model;
ui->quickWidget->rootContext()->setContextProperty("markerModel", &model);
ui->quickWidget->setSource(QUrl::fromLocalFile(":/map.qml"));
another part:
void MainWindow::on_saveRSU_clicked()
{
std::unique_ptr<rsu> baz(new rsu);
baz->id = ui->ID->text().toStdString();
baz->name = ui->Name->text().toStdString();
baz->coorx = ui->CoorX->text().toFloat();
baz->coory = ui->CoorY->text().toFloat();
baz->ip = ui->IP->text().toStdString();
baz->port = ui->Port->text().toInt();
r.push_back(*baz);
QListWidgetItem *rsuitem = new QListWidgetItem(QString::fromUtf8(baz->name.c_str()),ui->listRSU);
rsuitem->setFlags(rsuitem->flags() | Qt::ItemIsUserCheckable);
rsuitem->setCheckState(Qt::Unchecked);
clear_all();
enable_all();
ui->rsuWidget->hide();
emit sendCoord(baz->coorx,baz->coory);
}
map.qml:
import QtQuick 2.0
import QtLocation 5.6
import QtPositioning 5.6
import QtQuick.Window 2.0
Rectangle {
width: Screen.width
height: Screen.height
visible: true
Plugin {
id: mapPlugin
name: "mapboxgl"
}
Map {
id: mapview
anchors.fill: parent
plugin: mapPlugin
center: QtPositioning.coordinate(59.91, 10.75)
zoomLevel: 14
MapItemView{
model: markerModel
delegate: mapcomponent
}
}
Component {
id: mapcomponent
MapQuickItem {
id: marker
anchorPoint.x: image.width/4
anchorPoint.y: image.height
coordinate: position
sourceItem: Image {
id: image
source: "qrc:/../RSUNEWW/mm_20_red.png"
}
}
}
Connections {
ignoreUnknownSignals: true
function onSendCoord(x,y) {
target: markerModel
var coordinate = mapview.toCoordinate(x,y)
markerModel.addMarker(coordinate)
}
}
}
What should I do different I dont know, I don't get any errors when I start