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. How to dynamically add nodes to a QtreeView
Forum Updated to NodeBB v4.3 + New Features

How to dynamically add nodes to a QtreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
how to dynamica
4 Posts 2 Posters 3.7k Views 2 Watching
  • 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by VRonin
    #1

    0.0.4 ,
    0.1.0 ,
    0.1.6 ,
    0.1.7 ,
    0.2.9 ,
    0.3.3 ,
    0.3.5 ,

    List = [A, [B,C,D,E], [a,b,c,d,e]

    The Tree to be created is as follows

       A
         B
           d
             -leaf1
             -leaf2
             -leaf3
       A
         C
           a
             -leaf11
             -leaf22
             -leaf33
    

    Now I want to create the nonleaf at the beginning. I want to add the leaf nodes leaf1, leaf2,leaf3 and leaf11, leaf22, leaf33 dynamically when I expand d and a. Can some write a sample code how to add dynamically add leaf nodes in QtreeView

    How can I use canfetchmore and fetch more

    A 1 Reply Last reply
    1
    • Q Qt Enthusiast

      0.0.4 ,
      0.1.0 ,
      0.1.6 ,
      0.1.7 ,
      0.2.9 ,
      0.3.3 ,
      0.3.5 ,

      List = [A, [B,C,D,E], [a,b,c,d,e]

      The Tree to be created is as follows

         A
           B
             d
               -leaf1
               -leaf2
               -leaf3
         A
           C
             a
               -leaf11
               -leaf22
               -leaf33
      

      Now I want to create the nonleaf at the beginning. I want to add the leaf nodes leaf1, leaf2,leaf3 and leaf11, leaf22, leaf33 dynamically when I expand d and a. Can some write a sample code how to add dynamically add leaf nodes in QtreeView

      How can I use canfetchmore and fetch more

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @Qt-Enthusiast I would just catch the void QTreeView::expanded(const QModelIndex &index) signal and add your items there.

      As for adding items in code, it works the same whether it's on expand or in the beginning. Here is an example using QStandardItems:

      auto model = new QStandardItemModel();
      auto tree = new QTreeView(this);
      tree->setModel(model);
      
      // add items like so
      model->appendRow(new QStandardItem("leaf1"));
      

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      3
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        but my only question is

        I have QAbrtacItemModel and also expanded has to work only on
        small a and small d

        A 1 Reply Last reply
        0
        • Q Qt Enthusiast

          but my only question is

          I have QAbrtacItemModel and also expanded has to work only on
          small a and small d

          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @Qt-Enthusiast Ok so I used QStandardItemModel as an example, obviously adapt to your model. :)

          As for expanding only small "a" and "d", you add that to your expanded() function logic. You get the QModelIndex and then you can just check if it is the one you need to populate or not.. Something like:

          void MyWidget::somethingExpanded(const QModelIndex &index)
          {
             if (myModel->myItemName(index) == "a" || myModel->myItemName(index) == "d")
             {
                 // add your items to your model here
             }
          }
          

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          2

          • Login

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