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 add children to the QTree Widget
Qt 6.11 is out! See what's new in the release blog

how to add children to the QTree Widget

Scheduled Pinned Locked Moved Solved General and Desktop
67 Posts 3 Posters 27.6k Views 1 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.
  • ManiRonM ManiRon

    @mrjj but my size is two. I have inserted two data and i am trying to see the second data then it should work right?

    ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by
    #32

    @ManiRon OK SIR IT WORKED

    1 Reply Last reply
    1
    • mrjjM mrjj

      @ManiRon
      Yes, you access to high index.
      as u start with 1.
      and list start with zero.
      So that can crash.

      ManiRonM Offline
      ManiRonM Offline
      ManiRon
      wrote on last edited by
      #33

      @mrjj

      I have another doubt sir

      mrjjM 1 Reply Last reply
      0
      • ManiRonM ManiRon

        @mrjj

        I have another doubt sir

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #34

        @ManiRon
        super :)
        ask away :)

        ManiRonM 2 Replies Last reply
        0
        • mrjjM mrjj

          @ManiRon
          super :)
          ask away :)

          ManiRonM Offline
          ManiRonM Offline
          ManiRon
          wrote on last edited by
          #35

          @mrjj 0_1536571284463_1.jpg

          How to use this ? for this only i am trying

          mrjjM 1 Reply Last reply
          0
          • mrjjM mrjj

            @ManiRon
            super :)
            ask away :)

            ManiRonM Offline
            ManiRonM Offline
            ManiRon
            wrote on last edited by
            #36

            @mrjj What is my doubt is i want to add N number of children to a toplevelitem and one child to each child ? How can i add N Number of childern first

            1 Reply Last reply
            0
            • ManiRonM ManiRon

              @mrjj 0_1536571284463_1.jpg

              How to use this ? for this only i am trying

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #37

              @ManiRon
              Its a list of QTreeWidgetItems * ( pointers)
              So you must add them to a QList then give QList to tree.
              Its just a function to add multiple items at once.

              
              // top one
                  QTreeWidgetItem *treeItemROOT = new QTreeWidgetItem(ui->treeWidget);
                  treeItem->setText(0, name);
                  treeItem->setText(1, description);
              
              // first child
                  QTreeWidgetItem *treeItem = new QTreeWidgetItem();
                  treeItem->setText(0, name);
                  treeItem->setText(1, description);
              
                  treeItemROOT ->addChild(treeItem); // add child to the root
              
              ManiRonM 1 Reply Last reply
              1
              • mrjjM mrjj

                @ManiRon
                Its a list of QTreeWidgetItems * ( pointers)
                So you must add them to a QList then give QList to tree.
                Its just a function to add multiple items at once.

                
                // top one
                    QTreeWidgetItem *treeItemROOT = new QTreeWidgetItem(ui->treeWidget);
                    treeItem->setText(0, name);
                    treeItem->setText(1, description);
                
                // first child
                    QTreeWidgetItem *treeItem = new QTreeWidgetItem();
                    treeItem->setText(0, name);
                    treeItem->setText(1, description);
                
                    treeItemROOT ->addChild(treeItem); // add child to the root
                
                ManiRonM Offline
                ManiRonM Offline
                ManiRon
                wrote on last edited by
                #38

                @mrjj said in how to add children to the QTree Widget:

                treeItem

                thats what can u give any example code for that "Its a list of QTreeWidgetItems * ( pointers)
                So you must add them to a QList then give QList to tree."

                mrjjM 1 Reply Last reply
                0
                • ManiRonM ManiRon

                  @mrjj said in how to add children to the QTree Widget:

                  treeItem

                  thats what can u give any example code for that "Its a list of QTreeWidgetItems * ( pointers)
                  So you must add them to a QList then give QList to tree."

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #39

                  @ManiRon
                  QList<QTreeWidgetItem> mylist;
                  mylist.append( new QTreeWidgetItem); // might want to set some text also
                  ..
                  ...
                  call addChildren with mylist.

                  However, it wont help you build the tree, just other way of adding more than one item at a time.

                  ManiRonM 2 Replies Last reply
                  1
                  • mrjjM mrjj

                    @ManiRon
                    QList<QTreeWidgetItem> mylist;
                    mylist.append( new QTreeWidgetItem); // might want to set some text also
                    ..
                    ...
                    call addChildren with mylist.

                    However, it wont help you build the tree, just other way of adding more than one item at a time.

                    ManiRonM Offline
                    ManiRonM Offline
                    ManiRon
                    wrote on last edited by
                    #40

                    @mrjj

                    My idea was to insert a list of data as a children to a toplevelitem

                    1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @ManiRon
                      QList<QTreeWidgetItem> mylist;
                      mylist.append( new QTreeWidgetItem); // might want to set some text also
                      ..
                      ...
                      call addChildren with mylist.

                      However, it wont help you build the tree, just other way of adding more than one item at a time.

                      ManiRonM Offline
                      ManiRonM Offline
                      ManiRon
                      wrote on last edited by
                      #41

                      @mrjj

                      I gave like this QList<QTreeWidgetItem>m_Data;
                      m_Data.append(new QTreeWidgetItem);

                      it throwed error saying

                      error: no matching function for call to 'QList<QTreeWidgetItem>::append(QTreeWidgetItem*)'
                      m_Data.append(new QTreeWidgetItem);
                      ^

                      mrjjM 1 Reply Last reply
                      0
                      • ManiRonM ManiRon

                        @mrjj

                        I gave like this QList<QTreeWidgetItem>m_Data;
                        m_Data.append(new QTreeWidgetItem);

                        it throwed error saying

                        error: no matching function for call to 'QList<QTreeWidgetItem>::append(QTreeWidgetItem*)'
                        m_Data.append(new QTreeWidgetItem);
                        ^

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #42

                        Hi
                        List must be of pointers.
                        QList<QTreeWidgetItem *>
                        (u missed the *)

                        ManiRonM 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          Hi
                          List must be of pointers.
                          QList<QTreeWidgetItem *>
                          (u missed the *)

                          ManiRonM Offline
                          ManiRonM Offline
                          ManiRon
                          wrote on last edited by
                          #43

                          @mrjj

                          Now I gave like this sir,

                          QList<QTreeWidgetItem*>m_Data;
                          m_Data.append(new QTreeWidgetItem);

                          QString Data = 0;

                          for(int i=1;i<3;i++)
                          {
                          Data = QString("SubItem"+QString::number(i));
                          m_Data.append(Data.toLatin1().constData());
                          item->addChildren(m_Data);
                          }

                          it throws error
                          error: no matching function for call to 'QList<QTreeWidgetItem*>::append(const char*)'
                          m_Data.append(Data.toLatin1().constData());
                          ^

                          mrjjM 1 Reply Last reply
                          0
                          • ManiRonM ManiRon

                            @mrjj

                            Now I gave like this sir,

                            QList<QTreeWidgetItem*>m_Data;
                            m_Data.append(new QTreeWidgetItem);

                            QString Data = 0;

                            for(int i=1;i<3;i++)
                            {
                            Data = QString("SubItem"+QString::number(i));
                            m_Data.append(Data.toLatin1().constData());
                            item->addChildren(m_Data);
                            }

                            it throws error
                            error: no matching function for call to 'QList<QTreeWidgetItem*>::append(const char*)'
                            m_Data.append(Data.toLatin1().constData());
                            ^

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #44

                            hi
                            well u try to add strings to it.
                            m_Data.append(Data.toLatin1().constData());
                            and it wants a QTreeWidgetItem *

                            I think you mean something like

                            for(int i=1;i<3;i++)
                            {
                            Data = QString("SubItem"+QString::number(i));
                            m_Data.append(Data.toLatin1().constData());
                            QTreeWidgetItem* item2=new QTreeWidgetItem();
                            // set text etc on item
                            m_Data.append(item2);
                            item->addChildren(m_Data);
                            }
                            

                            but why do it liek that since u new it there anyway so just add it directly ?

                            1 Reply Last reply
                            1
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #45

                              How are you trying to add it ?

                              aaa
                                bbb
                                   ccc
                              

                              or

                              aaa
                               bbbb
                              ccc
                               ddd
                              
                              ManiRonM 2 Replies Last reply
                              0
                              • mrjjM mrjj

                                How are you trying to add it ?

                                aaa
                                  bbb
                                     ccc
                                

                                or

                                aaa
                                 bbbb
                                ccc
                                 ddd
                                
                                ManiRonM Offline
                                ManiRonM Offline
                                ManiRon
                                wrote on last edited by
                                #46
                                This post is deleted!
                                1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  How are you trying to add it ?

                                  aaa
                                    bbb
                                       ccc
                                  

                                  or

                                  aaa
                                   bbbb
                                  ccc
                                   ddd
                                  
                                  ManiRonM Offline
                                  ManiRonM Offline
                                  ManiRon
                                  wrote on last edited by ManiRon
                                  #47

                                  @mrjj 0_1536578415429_2.JPG

                                  Something like this , top level item = aaaa

                                  children = bbbb,
                                  cccc,
                                  dddd,
                                  eeee,
                                  and child of eeee is ffff

                                  mrjjM 1 Reply Last reply
                                  0
                                  • ManiRonM ManiRon

                                    @mrjj 0_1536578415429_2.JPG

                                    Something like this , top level item = aaaa

                                    children = bbbb,
                                    cccc,
                                    dddd,
                                    eeee,
                                    and child of eeee is ffff

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #48

                                    @ManiRon
                                    Ok.
                                    Nothing odd then :)
                                    Then you just need to keep aaaa pointer around and
                                    eeee so you can add child to that.

                                    ManiRonM 1 Reply Last reply
                                    1
                                    • mrjjM mrjj

                                      @ManiRon
                                      Ok.
                                      Nothing odd then :)
                                      Then you just need to keep aaaa pointer around and
                                      eeee so you can add child to that.

                                      ManiRonM Offline
                                      ManiRonM Offline
                                      ManiRon
                                      wrote on last edited by
                                      #49

                                      @mrjj sir i tried the way u mentioned but i got like this 1_1536636757572_4.jpg 0_1536636757570_3.jpg

                                      my code:

                                      QList<QTreeWidgetItem*>m_Data;
                                      m_Data.append(new QTreeWidgetItem);

                                      QString Data = 0;
                                      for(int i=1;i<3;i++)
                                      {
                                      Data = QString("SubItem"+QString::number(i));
                                      QTreeWidgetItem* item1 =new QTreeWidgetItem(ui->treeWidget);
                                      item1->setText(0,"data1");
                                      m_Data.append(item1);
                                      item1->setText(0,Data.toLatin1().constData());
                                      m_Data.append(item1);
                                      item->addChild(item1);
                                      item1->addChildren(m_Data);
                                      }

                                      mrjjM 1 Reply Last reply
                                      0
                                      • ManiRonM ManiRon

                                        @mrjj sir i tried the way u mentioned but i got like this 1_1536636757572_4.jpg 0_1536636757570_3.jpg

                                        my code:

                                        QList<QTreeWidgetItem*>m_Data;
                                        m_Data.append(new QTreeWidgetItem);

                                        QString Data = 0;
                                        for(int i=1;i<3;i++)
                                        {
                                        Data = QString("SubItem"+QString::number(i));
                                        QTreeWidgetItem* item1 =new QTreeWidgetItem(ui->treeWidget);
                                        item1->setText(0,"data1");
                                        m_Data.append(item1);
                                        item1->setText(0,Data.toLatin1().constData());
                                        m_Data.append(item1);
                                        item->addChild(item1);
                                        item1->addChildren(m_Data);
                                        }

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #50

                                        @ManiRon
                                        hi
                                        seems like blank texts. also i dont see u new item.??
                                        anyway, that loop looks so messy and hard to read
                                        if you use functions, it can look like this

                                        QTreeWidgetItem * addTreeRoot(QTreeWidget* treeWidget, QString name, QString description) {
                                            QTreeWidgetItem *treeItem = new QTreeWidgetItem(treeWidget);
                                            treeItem->setText(0, name);
                                            treeItem->setText(1, description);
                                            return treeItem;
                                        }
                                        
                                        QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent,QString name, QString description) {
                                            QTreeWidgetItem *treeItem = new QTreeWidgetItem();
                                            treeItem->setText(0, name);
                                            treeItem->setText(1, description);
                                            parent->addChild(treeItem);
                                            return treeItem;
                                        }
                                        --
                                        
                                        QTreeWidgetItem* root=addTreeRoot(ui->treeWidget,"ROOT","text");
                                        addTreeChild(root,"bbbb","text");
                                        addTreeChild(root,"ccc","text");
                                        addTreeChild(root,"dddd","text");
                                        QTreeWidgetItem* last=addTreeChild(root,"eeee","text");
                                        
                                        addTreeChild(last,"eeeee","text"); // notice last as parent
                                        
                                        
                                        

                                        alt text

                                        ManiRonM 1 Reply Last reply
                                        1
                                        • mrjjM mrjj

                                          @ManiRon
                                          hi
                                          seems like blank texts. also i dont see u new item.??
                                          anyway, that loop looks so messy and hard to read
                                          if you use functions, it can look like this

                                          QTreeWidgetItem * addTreeRoot(QTreeWidget* treeWidget, QString name, QString description) {
                                              QTreeWidgetItem *treeItem = new QTreeWidgetItem(treeWidget);
                                              treeItem->setText(0, name);
                                              treeItem->setText(1, description);
                                              return treeItem;
                                          }
                                          
                                          QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent,QString name, QString description) {
                                              QTreeWidgetItem *treeItem = new QTreeWidgetItem();
                                              treeItem->setText(0, name);
                                              treeItem->setText(1, description);
                                              parent->addChild(treeItem);
                                              return treeItem;
                                          }
                                          --
                                          
                                          QTreeWidgetItem* root=addTreeRoot(ui->treeWidget,"ROOT","text");
                                          addTreeChild(root,"bbbb","text");
                                          addTreeChild(root,"ccc","text");
                                          addTreeChild(root,"dddd","text");
                                          QTreeWidgetItem* last=addTreeChild(root,"eeee","text");
                                          
                                          addTreeChild(last,"eeeee","text"); // notice last as parent
                                          
                                          
                                          

                                          alt text

                                          ManiRonM Offline
                                          ManiRonM Offline
                                          ManiRon
                                          wrote on last edited by
                                          #51

                                          @mrjj i am not getting sir,
                                          how i will declare the these functions?

                                          mrjjM 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