1

我正在尝试从 C++ 中的信号更新 QML 中的 MapCircle,但我整天都遇到了几个问题。

在我的课堂上,我有一个Q_PROPERTY只读的,它保存了 4 个无人机的 GPS 位置QVariantList

class GCS: public QObject
{
    Q_PROPERTY(QVariantList getUavPosition READ getUavPosition NOTIFY uavPositionSet)

    public:
      QVariantList getUavPosition() ; 
    signals:
      void uavPositionSet();
     public slots:
       void setUavPosition();
       void triggerPosition();
     private: 
       QVariantList connected_uavs;
       QVector<QGeoCoordinate> uav_positions;         

};

然后我将函数定义为:

void GCS::setUavPosition()
{
  double i = 0.0;

     QGeoCoordinate uav_id;
     uav_id.setLatitude(0.5);
     uav_id.setLongitude(0.5 + i);
     uav_id.setAltitude(5);
     uav_positions.insert(0, uav_id);
     connected_uavs.append( QVariant::fromValue(QGeoCoordinate(uav_positions[0].latitude(), uav_positions[0].longitude())));

      i+=0.15;  
    emit uavPositionSet();

}

 QVariantList GCS::getUavPosition() 
 {
   return connected_uavs;
 }

void GCS::triggerPosition()
{
  setUavPosition();
  ROS_INFO("Pos trig");
}

在我的主要功能中,我连接triggerPosition到一个计时器以便定期更新位置

int main(int argc, char *argv[])
{
     ros::init(argc, argv, "planner");
    QGuiApplication app(argc, argv);

     QQmlApplicationEngine engine;
     QQmlContext* context = engine.rootContext();
     GCS gcs;
     context->setContextProperty("planner", &gcs);    
     engine.load(QUrl(QStringLiteral("qrc:/planner.qml")));

     QTimer *timer = new QTimer();
     timer->setInterval(1000);
     QObject::connect(&gcs, SIGNAL(uavPositionSet()), &gcs, SLOT(setUavPosition()));
     QObject::connect(timer, SIGNAL(timeout()), &gcs, SLOT(triggerPosition()));
     timer->start();

    return app.exec();
}

但是,当我运行我的程序时,会有一点延迟,我mouseArea变得无法使用并且程序崩溃了。当我尝试打印经度以查看它是否更新时,初始值会多次打印到终端,但随后程序崩溃并且MapCircle地图上不存在

My Qml 文件的相关部分如下所示:

 Map{
        id: map
        anchors.fill:parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(0.5, 0.5)
        zoomLevel:50

        MapCircle{
            id:uavPos
            radius:2
            color:'black'
            border.width:3   
        }

    Connections{
        id:uavConnection
        target: planner
        onUavPositionSet:{

        var data = planner.getUavPosition
        uavPos.center = QtPositioning.coordinate(data[0].latitude, data[0].longitude)
        console.log(data[0].longitude)
        }

        }

    }

有人可以在这里指出我正确的方向吗?

4

1 回答 1

1

如果您要处理来自多个元素的信息,那么最好使用模型(与中继器一起创建多个元素),因此只需修改项目的角色:

gcs.h

#ifndef GCS_H
#define GCS_H

#include <QObject>

class QStandardItemModel;
class QAbstractItemModel;

class GCS: public QObject
{
    Q_OBJECT
    Q_PROPERTY(QObject* uavModel READ uavModel CONSTANT)
public:
    enum UAVRoles {
        PositionRole = Qt::UserRole + 1000
    };
    GCS(QObject *parent=nullptr);
    QObject *uavModel() const;
public Q_SLOTS:
    void triggerPosition();
private:
    QStandardItemModel* m_uavModel;
};

#endif // GCS_H

gcs.cpp

#include "gcs.h"

#include <QGeoCoordinate>
#include <QStandardItemModel>

#include <random>

GCS::GCS(QObject *parent):
    QObject(parent), m_uavModel(new QStandardItemModel(this))
{
    m_uavModel->setItemRoleNames({{PositionRole, "position"}});
    for(int i =0; i < 4; i++){
        QStandardItem *item = new QStandardItem;
        item->setData(QVariant::fromValue(QGeoCoordinate()), PositionRole);
        m_uavModel->appendRow(item);
    }
}

QObject *GCS::uavModel() const{
    return m_uavModel;
}

void GCS::triggerPosition(){
    std::mt19937 rng;
    rng.seed(std::random_device()());
    std::normal_distribution<> dist(-0.0001, +0.0001);

    if(QStandardItem *item = m_uavModel->item(0)){
        QGeoCoordinate uav_id;
        uav_id.setLatitude(0.5 + dist(rng));
        uav_id.setLongitude(0.5 + dist(rng));
        uav_id.setAltitude(5);
        item->setData(QVariant::fromValue(uav_id), PositionRole);
    }
}

主文件

#include "gcs.h"

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTimer>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    GCS gcs;

    QQmlApplicationEngine engine;
    QQmlContext* context = engine.rootContext();
    context->setContextProperty("planner", &gcs);
    const QUrl url(QStringLiteral("qrc:/planner.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    QTimer timer;
    timer.setInterval(1000);
    QObject::connect(&timer, &QTimer::timeout, &gcs, &GCS::triggerPosition);
    timer.start();

    return app.exec();
}

规划师.qml

import QtQuick 2.14
import QtQuick.Window 2.14
import QtLocation 5.14
import QtPositioning 5.14

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Plugin {
        id: mapPlugin
        name: "osm"
    }

    Map{
        id: map
        anchors.fill:parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(0.5, 0.5)
        zoomLevel:50

        MapItemView{
            model: planner.uavModel
            delegate: MapCircle{
                id:uavPos
                radius: 2
                color:'black'
                border.width:3
                center: QtPositioning.coordinate(model.position.latitude, model.position.longitude)
            }
        }
    }
}
于 2020-04-15T23:37:40.910 回答