As per the below code, I need to create a model which will have N number of "System", and each "System" will have N number of "SytemDatabase" and each "SytemDatabase" will have N number of "CoresData". This N number will come to know during the launch time of application.
struct CoresData {
int m_iCoreSpeed;
bool m_bCoreAvailable;
};
class SytemDatabase {
public:
SytemDatabase();
bool m_bDatabaseVisible;
int m_iDatabaseNumber;
QList<CoresData> m_listCoresData;
};
class Sytem {
public:
Sytem();
bool m_bSystemAvailable;
int m_iSystemNumber;
QList<SytemDatabase> m_listSytemDatabase;
};
class SytemTree : public QAbstractItemModel {
Q_OBJECT
public:
explicit SytemTree( QObject *parent = nullptr);
~SytemTree();
QVariant data(const QModelIndex &index, int role) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
private:
void addSytemDatabase(Sytem &data);
QList<Sytem> m_listSystem;
};
Currently I have designed this model using a ListModel and its working fine. Now for some reason i need to move this model to cpp side and pass that model to qml. Below is the QML code for reference
SytemTree.append({ "iSystemNumber": systemNumber, "bSystemAvailable": false, "SytemDatabase":[{ "iDatabaseNumber": databaseNumber, "bDatabaseVisible": false,"CoresData": []}]})
for( var lp = 0; lp < totalcoreData; ++lp) {
SytemTree.get(systemNumber).SytemDatabase.get(iDatabaseNumber).CoresData.append({ "bCoreAvailable": true, "bCoreAvailable": true, "iCoreNumber": coreNumber})
}
consider the model is developed in cpp side with reference to above cpp classes Sytem, SytemDatabase and CoresData , and the same model is passed and used like below in the qml code
Repeater {
id: repeaterRootSystem
model: SytemTree
delegate: customRectangle {---
visible: systemAvailable
value: systemNumber
----
Repeater {
id: repeaterDatabase
model: SytemTree.get(index).SytemDatabase
delegate: customRectangle {---
visible: databaseVisible
value: databaseNumber
---
Repeater {
id: repeaterCoresData
model: SytemTree.get(index).SytemDatabase.get(index).CoresData
delegate: customRectangle {--
visible: coreAvailable
value: coreNumber
speed: coreSpeed
----
}
}
}
}
}
}
I have gone through concepts QAbstractListModel, QAbstractItemModel and QAbstractTableModel. But am looking for a model like one list model, and each list element will contain a tree like structure as mentioned above in the classes. Requesting anyone to suggest how to create a model which will have tree like structure. and which QAbstractxxxxxmodel will be correct to implement this concept. and in QML side i want to access the data through the index untill coresData , same like above QML code.
Thanks in advance.