gpt4 book ai didi

c++ - 阅读 QML 中结构的 C++ QVector

转载 作者:搜寻专家 更新时间:2023-10-31 00:26:22 26 4
gpt4 key购买 nike

在我的 C++ 课上我有

struct trackPoint {
QString lat;
QString lon;
QString elevation;
};

QVector<trackPoint> trackPoints;

在 QML 中,我想将其作为经纬度对的多维数组进行访问

[[0,1],[1,1],[2,1]]

这可以使用 Q_Property 机制吗?因为我很确定结构不能暴露给 QML?

我打平了:-

 Q_PROPERTY(QVector<trackPoint> trackPoints READ gpx)

用一个方法:-

QVector<trackPoint> GPXFileIO::gpx() const {
return trackPoints;
}

但这给了我错误:-

QMetaProperty::read: Unable to handle unregistered datatype 'QVector<trackPoint>' for property 'GPXFileIO::trackPoints'

最佳答案

将结构暴露给 QML 的一种简单方法是使用 Q_GADGET 和 Q_PROPERTY,这样我们就可以获得结构的每个元素,它们不会成为数组的一部分。另一方面,QVector 支持 QString、int、QUrl 等元素,但不支持新类型,在这种情况下,应该使用 QVariantList。

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QVector>

struct TrackPoint {
Q_GADGET
Q_PROPERTY(qreal lat MEMBER lat)
Q_PROPERTY(qreal lon MEMBER lon)
Q_PROPERTY(qreal elevation MEMBER elevation)
public:
qreal lat;
qreal lon;
qreal elevation;
};

class TrackClass: public QObject
{
Q_OBJECT
Q_PROPERTY(QVariantList trackpoints READ gpx)
public:
TrackClass(QObject *parent=nullptr):QObject(parent){
trackPoints << TrackPoint{10, 10, 10} << TrackPoint{11, 11, 11};
}
QVariantList gpx() const{
QVariantList l;
for(const TrackPoint & p: trackPoints){
l << QVariant::fromValue(p);
}
return l;
}
private:
QVector<TrackPoint> trackPoints;
};

int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
TrackClass track;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("track", &track);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}

#include "main.moc"

ma​​in.qml

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Component.onCompleted: {
for( var i in track.trackpoints){
var p = track.trackpoints[i];
console.log("lat: ", p.lat, "lon: ", p.lon, "elevation: ", p.elevation)
}
}
}

输出:

qml: lat:  10 lon:  10 elevation:  10
qml: lat: 11 lon: 11 elevation: 11

关于c++ - 阅读 QML 中结构的 C++ QVector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52806679/

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