Results 1 to 6 of 6

Thread: Creating tree kind of model from QAbstractItemModel or QAbstractListModel

  1. #1
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Creating tree kind of model from QAbstractItemModel or QAbstractListModel

    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.
    Qt Code:
    1. struct CoresData {
    2. int m_iCoreSpeed;
    3. bool m_bCoreAvailable;
    4. };
    5.  
    6. class SytemDatabase {
    7. public:
    8. SytemDatabase();
    9. bool m_bDatabaseVisible;
    10. int m_iDatabaseNumber;
    11. QList<CoresData> m_listCoresData;
    12. };
    13.  
    14. class Sytem {
    15. public:
    16. Sytem();
    17. bool m_bSystemAvailable;
    18. int m_iSystemNumber;
    19. QList<SytemDatabase> m_listSytemDatabase;
    20. };
    21.  
    22.  
    23.  
    24. class SytemTree : public QAbstractItemModel {
    25. Q_OBJECT
    26. public:
    27. explicit SytemTree( QObject *parent = nullptr);
    28. ~SytemTree();
    29. QVariant data(const QModelIndex &index, int role) const override;
    30. Qt::ItemFlags flags(const QModelIndex &index) const override;
    31. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    32. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    33. QModelIndex parent(const QModelIndex &index) const override;
    34. int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    35. int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    36.  
    37. private:
    38. void addSytemDatabase(Sytem &data);
    39. QList<Sytem> m_listSystem;
    40. };
    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

    Qt Code:
    1. SytemTree.append({ "iSystemNumber": systemNumber, "bSystemAvailable": false, "SytemDatabase":[{ "iDatabaseNumber": databaseNumber, "bDatabaseVisible": false,"CoresData": []}]})
    2.  
    3. for( var lp = 0; lp < totalcoreData; ++lp) {
    4. SytemTree.get(systemNumber).SytemDatabase.get(iDatabaseNumber).CoresData.append({ "bCoreAvailable": true, "bCoreAvailable": true, "iCoreNumber": coreNumber})
    5. }
    To copy to clipboard, switch view to plain text mode 

    In qml side i want to use this model as like below
    Qt Code:
    1. Repeater {
    2. id: repeaterRootSystem
    3. model: SytemTree
    4. delegate: Rectangle {--
    5. ----
    6. }
    7. Repeater {
    8. id: repeaterDatabase
    9. model: SytemTree.get(index).SytemDatabase
    10. delegate: Rectangle {---
    11. ---
    12. }
    13. Repeater {
    14. id: repeaterCoresData
    15. model: SytemTree.get(index).SytemDatabase.get(index).CoresData
    16. delegate: Rectangle {--
    17. ----
    18. }
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    497
    Thanked 74 Times in 70 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating tree kind of model from QAbstractItemModel or QAbstractListModel

    Since you want a tree it should be obvious that a QListModel and QTableModel can not work. You have to go with a QAbstractItemModel. For a simple tree model see here: https://doc.qt.io/qt-5/qtwidgets-ite...l-example.html

  3. The following user says thank you to ChristianEhrlicher for this useful post:

    gowreesh (13th January 2020)

  4. #3
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating tree kind of model from QAbstractItemModel or QAbstractListModel

    Thank you very much for the reply.
    I have gone through the link you have shared. But really is it possible to implement the model like below qml code consider the model is developed in cpp side with reference to above cpp classes Sytem, SytemDatabase and CoresData.

    Qt Code:
    1. Repeater {
    2. id: repeaterRootSystem
    3. model: SytemTree
    4. delegate: customRectangle {---
    5. visible: systemAvailable
    6. value: systemNumber
    7. ----
    8. Repeater {
    9. id: repeaterDatabase
    10. model: SytemTree.get(index).SytemDatabase
    11. delegate: customRectangle {---
    12. visible: databaseVisible
    13. value: databaseNumber
    14. ---
    15. Repeater {
    16. id: repeaterCoresData
    17. model: SytemTree.get(index).SytemDatabase.get(index).CoresData
    18. delegate: customRectangle {--
    19. visible: coreAvailable
    20. value: coreNumber
    21. speed: coreSpeed
    22. ----
    23. }
    24. }
    25. }
    26. }
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,632
    Thanks
    256
    Thanked 737 Times in 727 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Creating tree kind of model from QAbstractItemModel or QAbstractListModel

    But really is it possible to implement the model like below qml code consider the model is developed in cpp side with reference to above cpp classes Sytem, SytemDatabase and CoresData.
    Yes. The QAbstractItemModel is simply a wrapper for your internal data structure that allows the tree to be displayed in a QTreeView or filtered by proxies, etc.

    So if you already have a C++ tree-shaped data structure, then you must derive a class from QAbstractItemModel that is a mirror for that structure. In your model's data(), rowCount(), columnCount(), and other virtual methods you must implement, you will return whatever information is appropriate for that level in the tree.

    Implementing a tree model is not always easy, especially when different levels of the tree represent different C++ data structures. But it can be done.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    gowreesh (13th January 2020)

  7. #5
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating tree kind of model from QAbstractItemModel or QAbstractListModel

    I have 2 doubts ,
    1. Do I need to derive my all classes system,SytemDatabase and CoresData from QAbstractItemModel .
    2. Or Do i need to write like here in the example https://doc.qt.io/qt-5/qtwidgets-ite...l-example.html. please find the below code as per the example
    Qt Code:
    1. class CoresData {
    2. public:
    3. CoresData();
    4. ~CoresData();
    5.  
    6. void appendChild(CoresData *child);
    7. CoresData *child(int row);
    8. int childCount() const;
    9. int columnCount() const;
    10. QVariant data(int column) const;
    11. int row() const;
    12. CoresData *parentItem();
    13.  
    14. int m_iCoreSpeed;
    15. bool m_bCoreAvailable;
    16. int m_iCoreNumber;
    17. private:
    18. CoresData *m_parentItem;
    19. };
    20.  
    21. class SytemDatabase {
    22. public:
    23. SytemDatabase();
    24. ~SytemDatabase();
    25.  
    26. void appendChild(SytemDatabase *child);
    27. SytemDatabase *child(int row);
    28. int childCount() const;
    29. int columnCount() const;
    30. QVariant data(int column) const;
    31. int row() const;
    32. SytemDatabase *parentItem();
    33.  
    34. bool m_bDatabaseVisible;
    35. int m_iDatabaseNumber;
    36. private:
    37. QVector<SytemDatabase*> m_childItems;
    38. SytemDatabase *m_parentItem;
    39. QList<CoresData> m_listCoresData;
    40. };
    41.  
    42. class Sytem {
    43. public:
    44. Sytem();
    45. ~Sytem();
    46.  
    47. void appendChild(Sytem *child);
    48. Sytem *child(int row);
    49. int childCount() const;
    50. int columnCount() const;
    51. QVariant data(int column) const;
    52. int row() const;
    53. Sytem *parentItem();
    54.  
    55. bool m_bSystemAvailable;
    56. int m_iSystemNumber;
    57. private:
    58. QVector<Sytem*> m_childItems;
    59. Sytem *m_parentItem;
    60. QList<SytemDatabase> m_listSytemDatabase;
    61. };
    62.  
    63.  
    64.  
    65. class SytemTree : public QAbstractItemModel {
    66. Q_OBJECT
    67. public:
    68. explicit SytemTree( QObject *parent = nullptr);
    69. ~SytemTree();
    70. QVariant data(const QModelIndex &index, int role) const override;
    71. Qt::ItemFlags flags(const QModelIndex &index) const override;
    72. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    73. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    74. QModelIndex parent(const QModelIndex &index) const override;
    75. int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    76. int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    77.  
    78. private:
    79. void addSytemDatabase(Sytem &data);
    80. QList<Sytem> m_listSystem;
    81. };
    To copy to clipboard, switch view to plain text mode 

    because i need to consider this model usage in qml i.e

    Qt Code:
    1. --
    2. model: System
    3. -----
    4. model: SytemTree.get(index).SytemDatabase
    5. -----
    6. model: SytemTree.get(index).SytemDatabase.get(index).CoresData
    7. ----
    To copy to clipboard, switch view to plain text mode 

    and also in the 2nd doubt, how to handle this pointer m_parentItem?? Since different level of tree is having different structure
    Last edited by gowreesh; 13th January 2020 at 08:44.

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,632
    Thanks
    256
    Thanked 737 Times in 727 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Creating tree kind of model from QAbstractItemModel or QAbstractListModel

    1. Do I need to derive my all classes system,SytemDatabase and CoresData from QAbstractItemModel .
    No. You probably do not have to change any of your existing classes. What you need is to derive a QAbstractItemModel that -uses- those classes (and the hierarchy you have built with them) to retrieve the information that will be displayed in your UI.

    2. Or Do i need to write like here in the example
    Also no, because your tree does not have a hierarchy like TreeItem -> TreeItem -> TreeItem... where all entries at any level of the tree are the same. In your case, you have System -> SystemDatabase -> CoresData. So, the "parent" item pointer for CoresData should point to the instance of SystemDatabase that owns it, SystemDatabase's parent is System, etc.

    And it makes no sense to have a CoresData:: appendChild() method, since those are the leaves of your tree and can't have children. Likewise, SystemDatabase:: appendChild() should add a CoresData child, not a SystemDatabase child. Same for the System class - it has SystemDatabase children.

    Your SystemTree class looks good, except I would add the System list as QList< System * > instead of QList< System >. If you define it as QList< System >, then the lifetime of the list entries is defined by the lifetime of the SystemTree - when the SystemTree goes out of scope, the QList and its contents will be deleted. If you define it as QList< System * >, then you can create and manage the System instances independently of the tree model. When the SystemTree goes out of scope, only the QList will be destroyed, not the pointers stored in it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. The following user says thank you to d_stranz for this useful post:

    gowreesh (14th January 2020)

Similar Threads

  1. Replies: 8
    Last Post: 11th March 2016, 09:56
  2. Replies: 4
    Last Post: 11th October 2012, 19:47
  3. Creating a Tree Model from a QStandardModel
    By kaushal_gaurav in forum Qt Programming
    Replies: 3
    Last Post: 19th December 2008, 06:47
  4. Help needed in creating Tree Model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 5
    Last Post: 18th December 2008, 12:14
  5. Replies: 1
    Last Post: 6th July 2007, 12:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.