原文的博客写得比较清楚,可惜代码的排版有点不敢恭维,自己进行了缩进。 原文地址:https://blog.csdn.net/qq_35173114/article/details/80873842 在QML中,经常会用到ListView控件,我们主要用到MVC模式,下面介绍几种常用数据模型,其中包括QML和C++模型
ListModel是一个简单的ListElement容器,每个容器都包含数据角色。其中内容可以动态定义,也可以在QML中显式定义。
ListModel { id: m_model ListElement { name: "Bill Smith" number: "555 3264" color1: "red" } ListElement { name: "John Brown" number: "555 8426" color1: "green" } ListElement { name: "Sam Wise" number: "555 0473" color1: "blue" } } ListView { width: 100 height: 100 model: m_model delegate: Text{ color: color1 text: name+":"+number } }这样就指定了刚才的model,运行效果:
当ObjectModel被用于视图的时候,视图不再需要委托,因为对象模型已经包含了可视化的委托(项)
ObjectModel { id: itemModel Rectangle { height: 20; width: 80; Text { color: "red" text: "Bill Smith" + ":" + "555 3264" } } Rectangle { height: 20; width: 80; Text{ color: "green" text: "John Brown" + ":" + "555 8426" } } Rectangle { height: 20; width: 80; Text { color: "blue" text: "Sam Wise"+":" + "555 0473" } } }选择该数据模型:
ListView{ width: 100 height: 100 model: itemModel }运行效果:
在main.cpp中
QStringList a; a << "Bill Smith" << "John Brown" << "Sam Wise"; //QStringList model QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("name1", QVariant::fromValue(a));在qml文件中:
ListView{ width: 100 height: 100 model: name1 delegate: Text{ text: modelData } }运行效果:
在main.cpp中: 自定义头文件:dataobject.h
#ifndef DATAOBJECT_H #define DATAOBJECT_H #include <QObject> class DataObject : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(QString number READ number WRITE setNumber NOTIFY numberChanged) public: DataObject(QObject *parent = nullptr); DataObject(const QString &name,const QString &color,const QString &number,QObject *parent=nullptr); QString name()const; void setName(const QString &name); QString color()const; void setColor(const QString &color); QString number()const; void setNumber(const QString &number); signals: void nameChanged(); void colorChanged(); void numberChanged(); private: QString m_name; QString m_color; QString m_number; }; #endif // DATAOBJECT_H源文件:
#include "dataobject.h" DataObject::DataObject(QObject *parent) : QObject(parent) { } DataObject::DataObject(const QString &name, const QString &color, const QString &number, QObject *parent) :QObject(parent),m_name(name),m_color(color),m_number(number) { } QString DataObject::name() const { return m_name; } void DataObject::setName(const QString &name) { if(name!=m_name) m_name=name; emit nameChanged(); } QString DataObject::color() const { return m_color; } void DataObject::setColor(const QString &color) { if(color!=m_color) m_color=color; emit colorChanged(); } QString DataObject::number() const { return m_number; } void DataObject::setNumber(const QString &number) { if(number!=m_number) m_number=number; emit numberChanged(); }在main.cpp中
QList<QObject*> dataList; //QObject model dataList << new DataObject("Bill Smith","red","555 3264") << new DataObject("John Brown","green","555 8426") << new DataObject("Sam Wise","blue","555 0473"); QQmlApplicationEngine engine; //之前有定义的,这里就不用重复定义了 engine.rootContext()->setContextProperty("myObjectModel", QVariant::fromValue(dataList));qml文件中:
ListView{ width: 100 height: 100 model: myObjectModel delegate: Text{ color: model.modelData.color text: name+":"+number} }运行效果:
自定义头文件abstractlistmodel.h
#ifndef ABSTRACTLISTMODEL_H #define ABSTRACTLISTMODEL_H #include <QAbstractListModel> #include<QStringList> class AbstractList //抽象数据列表类 { public: AbstractList(const QString &name,const QString &color,const QString &number); QString name() const; QString color() const; QString number() const; private: QString m_name; QString m_color; QString m_number; }; class AbstractListModel : public QAbstractListModel { Q_OBJECT public: enum AbstractListRoles{ NameRole=Qt::UserRole+1, ColorRole, NumberRole }; //定义抽象类成员角色 AbstractListModel(QObject *parent=nullptr); void addList(const AbstractList &list); int rowCount(const QModelIndex &parent=QModelIndex()) const; //返回给定父项行数 QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;//返回索引所在项给定角色的存储数据 protected: QHash<int,QByteArray> roleNames() const; //返回模型的角色名 private: QList<AbstractList> m_abstractList; //抽象列表类容器 }; #endif // ABSTRACTLISTMODEL_H源文件:
#include "abstractlistmodel.h" AbstractListModel::AbstractListModel(QObject *parent) :QAbstractListModel(parent) { } void AbstractListModel::addList(const AbstractList &list) { beginInsertRows(QModelIndex(),rowCount(),rowCount()); m_abstractList.append(list); // m_abstractList<<list; endInsertRows(); } int AbstractListModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return m_abstractList.count(); } QVariant AbstractListModel::data(const QModelIndex &index, int role) const { if(index.row()<0||index.row()>=m_abstractList.count()) return QVariant(); const AbstractList &abstractList=m_abstractList[index.row()]; if(role==AbstractListRoles::NameRole) return abstractList.name(); else if(role==AbstractListRoles::ColorRole) return abstractList.color(); else if(role==AbstractListRoles::NumberRole) return abstractList.number(); return QVariant(); } QHash<int, QByteArray> AbstractListModel::roleNames() const { QHash<int,QByteArray> roles; //use operator[] // roles[AbstractListRoles::NameRole]="name"; //定义对应角色值 // roles[AbstractListRoles::ColorRole]="color1"; // roles[AbstractListRoles::NumberRole]="number"; //use insert roles.insert(AbstractListRoles::NameRole,"name"); roles.insert(AbstractListRoles::ColorRole,"color1"); roles.insert(AbstractListRoles::NumberRole,"number"); return roles; } AbstractList::AbstractList(const QString &name, const QString &color, const QString &number) :m_name(name),m_color(color),m_number(number) { } QString AbstractList::name() const { return m_name; } QString AbstractList::color() const { return m_color; } QString AbstractList::number() const { return m_number; }main.cpp中
AbstractListModel listmodel; listmodel.addList(AbstractList("Bill Smith","red","555 3264")); listmodel.addList(AbstractList("John Brown","green","555 8426")); listmodel.addList(AbstractList("Sam Wise","blue","555 0473")); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("myModel", &listmodel);qml文件中:
ListView{ width: 100 height: 100 model: myModel delegate: Text { color: color1 text: name+":"+number} } }运行效果: main.qml文件的所有代码:
import QtQuick 2.6 import QtQuick.Window 2.2 import QtQml.Models 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") ListModel { id: m_model ListElement { name: "Bill Smith" number: "555 3264" color1: "red" } ListElement { name: "John Brown" number: "555 8426" color1: "green" } ListElement { name: "Sam Wise" number: "555 0473" color1: "blue" } } ObjectModel { id: itemModel Rectangle { height: 20; width: 80; Text { color: "red" text: "Bill Smith" + ":" + "555 3264" } } Rectangle { height: 20; width: 80; Text{ color: "green" text: "John Brown" + ":" + "555 8426" } } Rectangle { height: 20; width: 80; Text { color: "blue" text: "Sam Wise"+":" + "555 0473" } } } Column{ spacing: 10 ListView{ width: 100 height: 100 model: m_model delegate: Text{ color: color1 text: name+":"+number } } ListView{ width: 100 height: 100 model: itemModel } ListView{ width: 100 height: 100 model: name1 delegate: Text{ text: modelData } } ListView{ width: 100 height: 100 model: myObjectModel delegate: Text{ color: model.modelData.color text: name+":"+number} } ListView{ width: 100 height: 100 model: myModel delegate: Text { color: color1 text: name+":"+number} } } }main.cpp文件:
#include <QGuiApplication> #include <QQmlApplicationEngine> #include<QQmlContext> #include"dataobject.h" #include"abstractlistmodel.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QStringList a; a << "Bill Smith" << "John Brown" << "Sam Wise"; //QStringList model QList<QObject*> dataList; //QObject model dataList << new DataObject("Bill Smith","red","555 3264") << new DataObject("John Brown","green","555 8426") << new DataObject("Sam Wise","blue","555 0473"); AbstractListModel listmodel; listmodel.addList(AbstractList("Bill Smith","red","555 3264")); listmodel.addList(AbstractList("John Brown","green","555 8426")); listmodel.addList(AbstractList("Sam Wise","blue","555 0473")); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("name1", QVariant::fromValue(a)); engine.rootContext()->setContextProperty("myObjectModel", QVariant::fromValue(dataList)); engine.rootContext()->setContextProperty("myModel", &listmodel); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }原文地址:https://blog.csdn.net/qq_35173114/article/details/80873842