将 Exposed 模型集成到 ListModel QML 中
Integration of Exposed model into ListModel QML
我已经从 C++ 向我的 main.qml 公开了一个包含 3 个整数的列表
在 main.qml
中使用 ListView
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_
}}
我真正需要做的是将我的 C++ 模型集成到 ItemModelSurfaceDataProxy 中,它采用 ListModel,就像这样
ListModel {
id: myModel
ListElement{ longitude: "0"; latitude: "0"; height_: "124"; }
}}
main.qml 使用 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 使用 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 使用 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;
};
您应该在 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();
}
setInitialProperties
函数尝试设置对象的属性(在本例中是 'main.qml' 文件),如果您将 property var model
添加到 'main.qml' 它可能会起作用.
PS,如果你不想混淆属性 'height',你可以使用'altitude' ;-)
感谢@Amfasis
它通过在 rootContext
上设置 属性 来工作
viewer.rootContext()->setContextProperty("model", &model);
并在 Item
下添加了 属性 var 模型
Item {
property var model
....
}
我已经从 C++ 向我的 main.qml 公开了一个包含 3 个整数的列表 在 main.qml
中使用 ListViewListView {
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_
}}
我真正需要做的是将我的 C++ 模型集成到 ItemModelSurfaceDataProxy 中,它采用 ListModel,就像这样
ListModel {
id: myModel
ListElement{ longitude: "0"; latitude: "0"; height_: "124"; }
}}
main.qml 使用 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 使用 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 使用 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;
};
您应该在 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();
}
setInitialProperties
函数尝试设置对象的属性(在本例中是 'main.qml' 文件),如果您将 property var model
添加到 'main.qml' 它可能会起作用.
PS,如果你不想混淆属性 'height',你可以使用'altitude' ;-)
感谢@Amfasis 它通过在 rootContext
上设置 属性 来工作viewer.rootContext()->setContextProperty("model", &model);
并在 Item
下添加了 属性 var 模型Item {
property var model
....
}