0

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.

Here is my ui Here is my ui

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

Mete Korucu
  • 113
  • 12
  • What's a problem with this code? – folibis Sep 28 '22 at 13:23
  • I can't put marker on map – Mete Korucu Sep 28 '22 at 13:34
  • 1
    In your `Connections` object, the `target` line needs to be outside the function definition. – JarMan Sep 28 '22 at 17:23
  • 1
    Note that this is definitely not a [mcve], everything related to `QAbstractListModel`, the shown map, the rsu object is irrelevant. – m7913d Sep 28 '22 at 18:16
  • 1
    Does this answer your question? [How can I listen to a C++ signal from QML?](https://stackoverflow.com/questions/29995250/how-can-i-listen-to-a-c-signal-from-qml) – m7913d Sep 28 '22 at 18:21
  • 1
    Another approach is using `Q_PROPERTY`: https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html – m7913d Sep 28 '22 at 18:22
  • Thank you guys, but I cannot solve the problem. When I comment the line "ignoreUnknownSignals" I get the error "QML Connections: Cannot assign to non-existent property "onSendCoord" I think I can't connect the signal (I write target line to the outside) – Mete Korucu Sep 29 '22 at 10:02

0 Answers0