Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Create model for TreeView
QtWS25 Last Chance

Create model for TreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 639 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mihaill
    wrote on last edited by
    #1

    Hi!
    How I can create model for TreeView?
    If I do this, then I have only one level in tree:

    #ifndef BUILDINGTREEMODEL_H
    #define BUILDINGTREEMODEL_H
    
    #include <QObject>
    #include <QAbstractItemModel>
    #include "structures.h"
    
    class BuildingTreeModel : public QAbstractListModel
    {
        Q_OBJECT
    public:
        BuildingTreeModel(QObject *parent = nullptr);
        BuildingTreeModel(QSharedPointer<BuildingForMap> &building, QObject *parent = nullptr);
        ~BuildingTreeModel();
    
        enum MarkerRoles{idRole,
                        nameRole,
                        commentRole};
    
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        int columnCount(const QModelIndex &parent = QModelIndex()) const override;
        QHash<int, QByteArray> roleNames() const;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    
        void setBuilding(QSharedPointer<BuildingForMap> building);
    
    private:
        QSharedPointer<BuildingForMap> m_building, m_buildingForView;
    
    private slots:
        QVector<int> getDeepIndex(const QModelIndex &index) const; //from childs to parents
        QSharedPointer<BuildingForMap> getBuildingOnIndex(const QModelIndex &index)const;
    
    };
    
    #endif // BUILDINGTREEMODEL_H
    
    
    #include "buildingtreemodel.h"
    
    BuildingTreeModel::BuildingTreeModel(QObject *parent) : QAbstractListModel(parent)
    {
    
    }
    
    BuildingTreeModel::BuildingTreeModel(QSharedPointer<BuildingForMap> &building, QObject *parent) : QAbstractListModel(parent)
    {
        setBuilding(building);
    }
    
    BuildingTreeModel::~BuildingTreeModel()
    {
    
    }
    
    int BuildingTreeModel::rowCount(const QModelIndex &parent) const
    {
        QSharedPointer<BuildingForMap> building = getBuildingOnIndex(parent);
        qDebug()<<"-------------rowCount"<<building.data()->m_childrens.count()<<parent;
        return building.data()->m_childrens.count();
    }
    
    int BuildingTreeModel::columnCount(const QModelIndex &parent) const
    {
        return 1;
    }
    
    QHash<int, QByteArray> BuildingTreeModel::roleNames() const
    {
        QHash<int, QByteArray> roles;
        roles[idRole] = "id";
        roles[nameRole] = "name";
        roles[commentRole] = "comment";
        return roles;
    }
    
    QVariant BuildingTreeModel::data(const QModelIndex &index, int role) const
    {
        qDebug()<<"-------------data";
        QSharedPointer<BuildingForMap> building = getBuildingOnIndex(index);
        if (role== BuildingTreeModel::idRole) {
            return QVariant::fromValue(building.data()->m_id);
        } else if (role== BuildingTreeModel::nameRole) {
            return QVariant::fromValue(building.data()->m_name);
        } else if (role== BuildingTreeModel::commentRole) {
            return QVariant::fromValue(building.data()->m_comment);
        }
    
        return QVariant();
    }
    
    void BuildingTreeModel::setBuilding(QSharedPointer<BuildingForMap> building)
    {
        m_building = building;
        m_buildingForView = building;
    }
    
    QVector<int> BuildingTreeModel::getDeepIndex(const QModelIndex &index) const
    {
        QVector<int> deep;
        if (index.isValid()) {
            qDebug()<<"index"<<index;
            if (index.parent() == QModelIndex()) {
                deep.append(index.row());
                deep += getDeepIndex(index.parent());
            }
        }
        return deep;
    }
    
    QSharedPointer<BuildingForMap> BuildingTreeModel::getBuildingOnIndex(const QModelIndex &index) const
    {
        QSharedPointer<BuildingForMap> building = m_building;
        QVector<int> deep = getDeepIndex(index);
        qDebug()<<"deep"<<deep<<deep.count();
        qDebug()<<"building"<<building.data()->m_name;
        for (int i = deep.count()-1; i > -1; --i) {
            building = building.data()->m_childrens[i];
            qDebug()<<"building"<<building.data()->m_name;
        }
        return building;
    }
    
    
    
    
    JonBJ 1 Reply Last reply
    0
    • M Mihaill

      Hi!
      How I can create model for TreeView?
      If I do this, then I have only one level in tree:

      #ifndef BUILDINGTREEMODEL_H
      #define BUILDINGTREEMODEL_H
      
      #include <QObject>
      #include <QAbstractItemModel>
      #include "structures.h"
      
      class BuildingTreeModel : public QAbstractListModel
      {
          Q_OBJECT
      public:
          BuildingTreeModel(QObject *parent = nullptr);
          BuildingTreeModel(QSharedPointer<BuildingForMap> &building, QObject *parent = nullptr);
          ~BuildingTreeModel();
      
          enum MarkerRoles{idRole,
                          nameRole,
                          commentRole};
      
          int rowCount(const QModelIndex &parent = QModelIndex()) const override;
          int columnCount(const QModelIndex &parent = QModelIndex()) const override;
          QHash<int, QByteArray> roleNames() const;
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
      
          void setBuilding(QSharedPointer<BuildingForMap> building);
      
      private:
          QSharedPointer<BuildingForMap> m_building, m_buildingForView;
      
      private slots:
          QVector<int> getDeepIndex(const QModelIndex &index) const; //from childs to parents
          QSharedPointer<BuildingForMap> getBuildingOnIndex(const QModelIndex &index)const;
      
      };
      
      #endif // BUILDINGTREEMODEL_H
      
      
      #include "buildingtreemodel.h"
      
      BuildingTreeModel::BuildingTreeModel(QObject *parent) : QAbstractListModel(parent)
      {
      
      }
      
      BuildingTreeModel::BuildingTreeModel(QSharedPointer<BuildingForMap> &building, QObject *parent) : QAbstractListModel(parent)
      {
          setBuilding(building);
      }
      
      BuildingTreeModel::~BuildingTreeModel()
      {
      
      }
      
      int BuildingTreeModel::rowCount(const QModelIndex &parent) const
      {
          QSharedPointer<BuildingForMap> building = getBuildingOnIndex(parent);
          qDebug()<<"-------------rowCount"<<building.data()->m_childrens.count()<<parent;
          return building.data()->m_childrens.count();
      }
      
      int BuildingTreeModel::columnCount(const QModelIndex &parent) const
      {
          return 1;
      }
      
      QHash<int, QByteArray> BuildingTreeModel::roleNames() const
      {
          QHash<int, QByteArray> roles;
          roles[idRole] = "id";
          roles[nameRole] = "name";
          roles[commentRole] = "comment";
          return roles;
      }
      
      QVariant BuildingTreeModel::data(const QModelIndex &index, int role) const
      {
          qDebug()<<"-------------data";
          QSharedPointer<BuildingForMap> building = getBuildingOnIndex(index);
          if (role== BuildingTreeModel::idRole) {
              return QVariant::fromValue(building.data()->m_id);
          } else if (role== BuildingTreeModel::nameRole) {
              return QVariant::fromValue(building.data()->m_name);
          } else if (role== BuildingTreeModel::commentRole) {
              return QVariant::fromValue(building.data()->m_comment);
          }
      
          return QVariant();
      }
      
      void BuildingTreeModel::setBuilding(QSharedPointer<BuildingForMap> building)
      {
          m_building = building;
          m_buildingForView = building;
      }
      
      QVector<int> BuildingTreeModel::getDeepIndex(const QModelIndex &index) const
      {
          QVector<int> deep;
          if (index.isValid()) {
              qDebug()<<"index"<<index;
              if (index.parent() == QModelIndex()) {
                  deep.append(index.row());
                  deep += getDeepIndex(index.parent());
              }
          }
          return deep;
      }
      
      QSharedPointer<BuildingForMap> BuildingTreeModel::getBuildingOnIndex(const QModelIndex &index) const
      {
          QSharedPointer<BuildingForMap> building = m_building;
          QVector<int> deep = getDeepIndex(index);
          qDebug()<<"deep"<<deep<<deep.count();
          qDebug()<<"building"<<building.data()->m_name;
          for (int i = deep.count()-1; i > -1; --i) {
              building = building.data()->m_childrens[i];
              qDebug()<<"building"<<building.data()->m_name;
          }
          return building;
      }
      
      
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Mihaill
      To build a tree model you must use the parent to create the depth hierarchy. See how getDeepIndex() recurses on index.parent()? Where when you populate with data are you doing setting the parent of child nodes?

      However, you are using QAbstractListModel. And that says:

      Since the model provides a more specialized interface than QAbstractItemModel, it is not suitable for use with tree views; you will need to subclass QAbstractItemModel if you want to provide a model for that purpose

      !

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mihaill
        wrote on last edited by Mihaill
        #3

        Maybe you have simple example for tree model?
        I use recurses fo find deep tree. Maybe you know how need dooing better?

        JonBJ 1 Reply Last reply
        0
        • M Mihaill

          Maybe you have simple example for tree model?
          I use recurses fo find deep tree. Maybe you know how need dooing better?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Mihaill
          Google does! qt tree model example -> Simple Tree Model Example ?

          I use recurses fo find deep tree. Maybe you know how need dooing better?

          No, recursion is the right way to do this. That code is right. But it relies on you having set up correct parentage so that the tree correctly implements descendants.

          1 Reply Last reply
          2
          • M Offline
            M Offline
            Mihaill
            wrote on last edited by
            #5

            I can find this example in the code examples in QtCreator?

            JonBJ 1 Reply Last reply
            0
            • M Mihaill

              I can find this example in the code examples in QtCreator?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Mihaill I don't know, but probably yes. Have you tried to do so?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mihaill
                wrote on last edited by
                #7

                How I can find deep tree model, if I use my class for data?
                It is not work for me:

                int TreeModel::rowCount(const QModelIndex &parent) const
                {
                    TreeItem *parentItem;
                    if (parent.column() > 0)
                        return 0;
                
                    if (!parent.isValid())
                        parentItem = rootItem;
                    else
                        parentItem = static_cast<TreeItem*>(parent.internalPointer());
                
                    return parentItem->childCount();
                }
                
                
                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mihaill
                  wrote on last edited by
                  #8

                  Which methods still need to be overrided in QAbstractItemModel?

                  JonBJ 1 Reply Last reply
                  0
                  • M Mihaill

                    Which methods still need to be overrided in QAbstractItemModel?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @Mihaill
                    Everything is covered in the example quoted. Suggest you try that sample exactly as-is, understand it, get it working, and then make any modifications for whatever you want.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mihaill
                      wrote on last edited by
                      #10

                      With this code what me need dooing in index and parent?

                      #ifndef BUILDINGTREEMODEL_H
                      #define BUILDINGTREEMODEL_H
                      
                      #include <QObject>
                      #include <QAbstractItemModel>
                      #include "structures.h"
                      
                      class BuildingTreeModel : public QAbstractItemModel
                      {
                          Q_OBJECT
                      public:
                          BuildingTreeModel(QObject *parent = nullptr);
                          BuildingTreeModel(QSharedPointer<BuildingForMap> &building, QObject *parent = nullptr);
                          ~BuildingTreeModel();
                      
                          enum MarkerRoles{idRole,
                                          nameRole,
                                          commentRole};
                      
                          int rowCount(const QModelIndex &parent = QModelIndex()) const override;
                          int columnCount(const QModelIndex &parent = QModelIndex()) const override;
                          QHash<int, QByteArray> roleNames() const;
                          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
                          QModelIndex parent(const QModelIndex &index) const override;
                          QModelIndex index(int row, int column,
                                                const QModelIndex &parent = QModelIndex()) const override;
                      
                          void setBuilding(QSharedPointer<BuildingForMap> building);
                      
                      private:
                          QSharedPointer<BuildingForMap> m_building, m_buildingForView;
                      
                      private slots:
                          QVector<int> getDeepIndex(const QModelIndex &index) const; //from childs to parents
                          QSharedPointer<BuildingForMap> getBuildingOnIndex(const QModelIndex &index)const;
                      
                      };
                      
                      #endif // BUILDINGTREEMODEL_H
                      
                      
                      #include "buildingtreemodel.h"
                      
                      BuildingTreeModel::BuildingTreeModel(QObject *parent) : QAbstractItemModel(parent)
                      {
                      
                      }
                      
                      BuildingTreeModel::BuildingTreeModel(QSharedPointer<BuildingForMap> &building, QObject *parent) : QAbstractItemModel(parent)
                      {
                          setBuilding(building);
                      }
                      
                      BuildingTreeModel::~BuildingTreeModel()
                      {
                      
                      }
                      
                      int BuildingTreeModel::rowCount(const QModelIndex &parent) const
                      {
                          qDebug()<<"-------------rowCount1";
                          QSharedPointer<BuildingForMap> building = getBuildingOnIndex(parent);
                          qDebug()<<"-------------rowCount2"<<building.data()->m_childrens.count()<<parent;
                          return building.data()->m_childrens.count();
                      }
                      
                      int BuildingTreeModel::columnCount(const QModelIndex &parent) const
                      {
                          return 1;
                      }
                      
                      QHash<int, QByteArray> BuildingTreeModel::roleNames() const
                      {
                          QHash<int, QByteArray> roles;
                          roles[idRole] = "id";
                          roles[nameRole] = "name";
                          roles[commentRole] = "comment";
                          return roles;
                      }
                      
                      QVariant BuildingTreeModel::data(const QModelIndex &index, int role) const
                      {
                          qDebug()<<"-------------data";
                          QSharedPointer<BuildingForMap> building = getBuildingOnIndex(index);
                          if (role== BuildingTreeModel::idRole) {
                              return QVariant::fromValue(building.data()->m_id);
                          } else if (role== BuildingTreeModel::nameRole) {
                              return QVariant::fromValue(building.data()->m_name);
                          } else if (role== BuildingTreeModel::commentRole) {
                              return QVariant::fromValue(building.data()->m_comment);
                          }
                      
                          return QVariant();
                      }
                      
                      QModelIndex BuildingTreeModel::parent(const QModelIndex &index) const
                      {
                          qDebug()<<"---BuildingTreeModel::parent";
                          return index;
                      }
                      
                      QModelIndex BuildingTreeModel::index(int row, int column, const QModelIndex &parent) const
                      {
                      //    QModelIndex index(row, column, *parent, this);
                      //    QModelIndex index()
                          qDebug()<<"---BuildingTreeModel::index";
                          return parent;
                      }
                      
                      void BuildingTreeModel::setBuilding(QSharedPointer<BuildingForMap> building)
                      {
                          m_building = building;
                          m_buildingForView = building;
                      }
                      
                      QVector<int> BuildingTreeModel::getDeepIndex(const QModelIndex &index) const
                      {
                          QVector<int> deep;
                          if (index.isValid()) {
                              qDebug()<<"index"<<index;
                              if (index.parent() == QModelIndex()) {
                                  deep.append(index.row());
                                  deep += getDeepIndex(index.parent());
                              }
                          }
                          return deep;
                      }
                      
                      QSharedPointer<BuildingForMap> BuildingTreeModel::getBuildingOnIndex(const QModelIndex &index) const
                      {
                          QSharedPointer<BuildingForMap> building = m_building;
                          QVector<int> deep = getDeepIndex(index);
                          qDebug()<<"deep"<<deep<<deep.count();
                          qDebug()<<"building"<<building.data()->m_name;
                          for (int i = deep.count()-1; i > -1; --i) {
                              building = building.data()->m_childrens[i];
                              qDebug()<<"building"<<building.data()->m_name;
                          }
                          return building;
                      }
                      
                      
                      
                      
                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mihaill
                        wrote on last edited by
                        #11

                        And why i can't create index with parent in index(int row, int column, const QModelIndex &parent = QModelIndex())
                        return createIndex(row, column, parent )?

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved