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. Model for TreeView without parent() method.
QtWS25 Last Chance

Model for TreeView without parent() method.

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 689 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.
  • P Offline
    P Offline
    Pavel Romankov
    wrote on last edited by
    #5

    I have another model (library), that provides elements. I can get pointer to element, whitch have methods parents() and children(). These methods return vectors of pointers to other elements.
    I 'm trying not to create additional date structures at my side and operate only these pointers. But as I understand it is impossible?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Pavel Romankov
      wrote on last edited by Pavel Romankov
      #6

      I have a lot of elements. For example, I have 100 elements with 100 children for each, and 100 children for each children.
      If I decide to build tree before displaying it, I have to build tree with 1000000 elements in memory.
      But user can expand only one branch.
      In QTableView / QAbstractTableModel view requests data from model only for visible cells, and this is great. Because there is no need to process all 1000000 elements at once before displaying them.
      I'm searching for way to display tree in similar way.

      Christian EhrlicherC 1 Reply Last reply
      0
      • P Pavel Romankov

        I have a lot of elements. For example, I have 100 elements with 100 children for each, and 100 children for each children.
        If I decide to build tree before displaying it, I have to build tree with 1000000 elements in memory.
        But user can expand only one branch.
        In QTableView / QAbstractTableModel view requests data from model only for visible cells, and this is great. Because there is no need to process all 1000000 elements at once before displaying them.
        I'm searching for way to display tree in similar way.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #7

        @Pavel-Romankov said in Model for TreeView without parent() method.:

        I'm searching for way to display tree in similar way.

        You can create the items on demand here also - there is no difference between a QTreeView and QTableView here.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Pavel Romankov
          wrote on last edited by
          #8

          Can you give example with creating tree items on demand? In witch method I have to do this?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #9

            Where do you get your data from? How do you know how much children are under a parent?
            I would reimplement QAIM::canFetchMore() / fetchMore()

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Pavel Romankov
              wrote on last edited by Pavel Romankov
              #10

              I have external library witch has method:

              Element *getElement(QString elementName);
              

              and each element has methods

              QVector<Element *> parents();
              QVector<Element *> children();
              QString name();
              QString data();
              

              I need to build a tree for element and it would be great to create children nodes only after user expands a branch.

              Christian EhrlicherC 1 Reply Last reply
              0
              • P Pavel Romankov

                I have external library witch has method:

                Element *getElement(QString elementName);
                

                and each element has methods

                QVector<Element *> parents();
                QVector<Element *> children();
                QString name();
                QString data();
                

                I need to build a tree for element and it would be great to create children nodes only after user expands a branch.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #11

                @Pavel-Romankov said in Model for TreeView without parent() method.:

                I need to build a tree for element and it would be great to create children nodes only after user expands a branch.

                But which nodes do you need to create at all - you've all in your Element class so use this in your model.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Pavel Romankov
                  wrote on last edited by
                  #12

                  Is it necessarily to implement parent() method in my model?

                  But which nodes do you need to create at all - you've all in your Element class so use this in your model.

                  Could you explain a little more in detail? What methods I need to implement to provide data in right way?

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    bee65
                    wrote on last edited by bee65
                    #13

                    In QAbstractItemModel, the QModelIndex parent(const QModelIndex &index) const method is pure virtual, so yes you have to implement it.

                    But the question is, does the implementation of parent() have to be consistent with index()? I.e., for all values of row, col, idx: parent( index(row,col,idx) ) == idx.

                    I'm not sure, but I think it would be risking trouble if the model returns a different parent than the one that created it in the tree. So I think you do need an addition data structure to hold this information, forming a single-parent tree. You could create these node objects on demand, as they are needed in the implementation of index(). For example:

                    struct myNode
                    {
                       myNode( Element *e, myNode *p ) :
                         element(e), parent(p), children( e->children().size() )
                         {   }
                    
                       Element *element;
                       myNode * parent;
                       QVector<myNode*> children;
                    };
                    
                    QModelIndex myModel::index(int row, int column, const QModelIndex &index) const
                    {
                       auto *parentNode = static_cast<const myNode*>(index.internalPointer());
                       auto *myChild = parentNode->children[row];
                       if (myChild == nullptr) 
                       {
                           myChild = new myNode( parentNode->element->children()[row], parentNode );
                           parentNode->children[row] = myChild;
                       }
                       return createIndex(row, column, myChild );
                    }
                    

                    This myNode object is initialized with a vector of null pointers for the children, and they are only allocated when that child is needed by the TreeView. (the code is just to give you the gist, it probably has errors)

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Pavel Romankov
                      wrote on last edited by Pavel Romankov
                      #14

                      @bee65, @Christian-Ehrlicher Thank you very much. It helped me a lot!

                      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