This is a Abandoned repository, I made a smailiar one QmlObjectList
Expose a list of your customized C++ data structure to QML, easily access data from C++ and QML both sides.
-
Put
QmlListModel.h
into your project, add it into.pro
, and enable C++11 by addingQMAKE_CXXFLAGS += -std=c++11
. -
Create your data class which derivers from
QObject
.
class Apartment : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name MEMBER mName)
public:
QString mName;
}
- Create the list model class which derivers from
QmlListModel<Data>
, addingQ_OBJECT
andQML_LIST_MODEL
macro in the header of data model class.
#include "QmlListModel.h"
class CompanyModel : public QmlListModel<Apartment>
{
Q_OBJECT
QML_LIST_MODEL
};
- Registers module by qmlRegisterType.
-
The QmlListModel provides
getData
appendData
etc. functions to accessing the data list. -
Serialize and unserialize it into QByteArray or
JSON
.
The QmlListModelDemo create a nested data structrue like this:
CompanyModel
├── Apartment 1
│ ├── Member A
│ └── Member B
├── Apartment 2
│ ├── Member C
│ ├── Member D
│ └── Member E
└── Apartment 3