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;
};
Q_OBJECT
public:
explicit SytemTree
( QObject *parent
= nullptr
);
~SytemTree();
Qt
::ItemFlags flags
(const QModelIndex &index
) const override;
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
= Qt
::DisplayRole) const override;
private:
void addSytemDatabase(Sytem &data);
QList<Sytem> m_listSystem;
};
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;
};
To copy to clipboard, switch view to plain text mode
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 just a 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})
}
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})
}
To copy to clipboard, switch view to plain text mode
In qml side i want to use this model as like below
Repeater {
id: repeaterRootSystem
model: SytemTree
delegate: Rectangle {--
----
}
Repeater {
id: repeaterDatabase
model: SytemTree.get(index).SytemDatabase
delegate: Rectangle {---
---
}
Repeater {
id: repeaterCoresData
model: SytemTree.get(index).SytemDatabase.get(index).CoresData
delegate: Rectangle {--
----
}
Repeater {
id: repeaterRootSystem
model: SytemTree
delegate: Rectangle {--
----
}
Repeater {
id: repeaterDatabase
model: SytemTree.get(index).SytemDatabase
delegate: Rectangle {---
---
}
Repeater {
id: repeaterCoresData
model: SytemTree.get(index).SytemDatabase.get(index).CoresData
delegate: Rectangle {--
----
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks