0

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.

  • The structure of the model (which in your case is a tree) does not determine how the information is shown, so in your case it is not clear to me that you want to obtain the part of the view. – eyllanesc Jan 09 '20 at 19:37
  • 2
    I would suggest you use `QStandardItemModel` and get your QML working with that first. Then, if you need to (because performance, or because it's exercise, or whatever), create custom model. It will be a great help to have a working reference version of the application. – hyde Jan 09 '20 at 19:56
  • I have updated my thread by adding some more information. Currently this model i have designed in QML using ListModel and its working fine . Now i want to move it to Cpp side. Please refer to the above QML code. So only I am looking for Qabstractmodels examples and confused which one to choose and how to develop for tree like structure which contains different class object data. – Gowreesh Cr Jan 10 '20 at 10:49

1 Answers1

0

Check out this similar question for some ideas.

If you're not adamant about using a QAbstractItemModel, here is a qml-based solution I've used before:

import QtQuick 2.9
import QtQuick.Window 2.2
import UISettings 1.0
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 1.4 as SV



Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Flickable {
        id: flick
        anchors.fill: parent
        clip: true
        contentHeight: col.implicitHeight
        property var mymodel: {
            "animals": {
                "big": {
                    "land": "elephants",
                    "water": "whales"
                },
                "small": {
                    "land": "mice",
                    "water": "fish"
                }
            },
            "plants": {
                "trees": "evergreens"
            }
        }

        Column {
            id: col
            Component.onCompleted: componentListView.createObject(this, {"objmodel":flick.mymodel});
        }

        Component {
            id: componentListView
            Repeater {
                id: repeater
                property var objmodel: ({})
                model: Object.keys(objmodel)

                ColumnLayout {
                    Layout.leftMargin: 50
                    Button {
                        property var sprite: null
                        text: modelData
                        onClicked: {
                            if(sprite === null) {
                                if(typeof objmodel[modelData] === 'object')
                                sprite = componentListView.createObject(parent, {"objmodel":objmodel[modelData]});
                            }
                            else
                                sprite.destroy()

                        }
                    }
                }
            }
        }
    }
}
Tyler M
  • 352
  • 4
  • 21
  • Hello Tyler M, thank you very much for the reply. As I mentioned in the thread above, I have a working qml list model but now I want to move this model to cpp side and pass the same model to qml. Requesting to provide a solution to create a tree kind of model. Basically same qml model which I mentioned above need to be developed in cpp – Gowreesh Cr Jan 12 '20 at 06:55