I have exposed successfully a list that contain 3 integer from C++ to my main.qml using ListView in main.qml
ListView {
width: 200; height: 250
required model
delegate: Text {
required property int longitude
required property int latitude
required property int height_
text: "Data: " + longitude + ", " + latitude +","+ height_
}}
What I actually need to do is to integrate my C++ model into ItemModelSurfaceDataProxy that takes ListModel like this
ListModel {
id: myModel
ListElement{ longitude: "0"; latitude: "0"; height_: "124"; }
}}
main.qml using qtdatavisualization-qmlsurface-example ,,example_source_code
Rectangle {
id: mainview
width: 1024
height: 768
color: surfacePlot.theme.windowColor
Item {
id: surfaceView
width: mainview.width
height: mainview.height
anchors.top: mainview.top
anchors.left: mainview.left
Surface3D {
id: surfacePlot
width: surfaceView.width
height: surfaceView.height
Surface3DSeries {
id: surfaceSeries
flatShadingEnabled: false
drawMode: Surface3DSeries.DrawSurface
ItemModelSurfaceDataProxy {
itemModel: myModel **// this needs to be replaced by C++ model**
rowRole: "longitude"
columnRole: "latitude"
yPosRole: "height_"
}
onDrawModeChanged: checkState()
}
}
ListModel { **//This is the model that needs to be replaced by C++ Model**
id: myModel
}
}}
main.cpp
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
AnimalModel model;
model.addAnimal(Animal(1, 2,3));
model.addAnimal(Animal(2,4,5));
model.addAnimal(Animal(3,4,6));
QQuickView viewer;
viewer.setResizeMode(QQuickView::SizeRootObjectToView);
viewer.setInitialProperties({{"model", QVariant::fromValue(&model)}});
viewer.setSource(QUrl("qrc:/qml/qml/qmlsurface/main.qml"));
viewer.setTitle(QStringLiteral("Egyptolict "));
viewer.show();
return app.exec();
}
model.cpp using QT example
#include "model.h"
Animal::Animal(const int &longitude, const int &latitude,const int &height_)
: m_longitude(longitude), m_latitude(latitude) ,m_height(height_){}
int Animal::longitude() const{
return m_longitude;}
int Animal::latitude() const{
return m_latitude;}
int Animal::height_() const{
return m_height;}
AnimalModel::AnimalModel(QObject *parent)
: QAbstractListModel(parent)
{
}
void AnimalModel::addAnimal(const Animal &animal)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_animals << animal;
endInsertRows();
}
int AnimalModel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
return m_animals.count();
}
QVariant AnimalModel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= m_animals.count())
return QVariant();
const Animal &animal = m_animals[index.row()];
if (role == longitudeRole)
return animal.longitude();
else if (role == latitudeRole)
return animal.latitude();
else if (role == heightRole)
return animal.height_();
return QVariant();
}
QHash<int, QByteArray> AnimalModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[longitudeRole] = "longitude";
roles[latitudeRole] = "latitude";
roles[heightRole] = "height_";
return roles;
}
model.h using qt example
#include <QAbstractListModel>
#include <QStringList>
class Animal
{public:
Animal(const int &longitude, const int &latitude , const int &height_);
int longitude() const;
int latitude() const;
int height_() const;
private:
int m_longitude;
int m_latitude;
int m_height;
};
class AnimalModel : public QAbstractListModel
{
Q_OBJECT
public:
enum AnimalRoles {
longitudeRole = Qt::UserRole + 1,
latitudeRole,
heightRole
};
AnimalModel(QObject *parent = 0);
void addAnimal(const Animal &animal);
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const;
private:
QList<Animal> m_animals;
};
You should set the property on the rootContext
:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
AnimalModel model;
model.addAnimal(Animal(1, 2,3));
model.addAnimal(Animal(2,4,5));
model.addAnimal(Animal(3,4,6));
QQuickView viewer;
viewer.setResizeMode(QQuickView::SizeRootObjectToView);
viewer.rootContext()->setContextProperty("model", &model);
viewer.setSource(QUrl("qrc:/qml/qml/qmlsurface/main.qml"));
viewer.setTitle(QStringLiteral("Egyptolict "));
viewer.show();
return app.exec();
}
The setInitialProperties
function tries to set properties on the object (in this case you 'main.qml' file), had you added property var model
to 'main.qml' it could have worked.
PS, if you don't want to mix up property 'height', you could use 'altitude' ;-)