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. Qtreewidget prevent duplicate parents
QtWS25 Last Chance

Qtreewidget prevent duplicate parents

Scheduled Pinned Locked Moved General and Desktop
qtreewidgetqtreewidgetitem
5 Posts 2 Posters 2.8k 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.
  • B Offline
    B Offline
    Bart
    wrote on 7 Jul 2015, 15:10 last edited by Bart 7 Jul 2015, 17:34
    #1

    Hi,

    I am currently trying to make a tree widget which takes items out of a multidimensional array (In this case ArraySet1[s1][3]). So I made some code and it adds the tree widget items fine but I don't want parents in the tree widget to be the same. If the same parents occur the childs of these parents should merge into one parent. Basicly I don't want duplicates of parents. I am just a beginner in C++ and Qt and my code is as following:

    for(int i = 0; i < s1; i++)
    {
        QTreeWidgetItem *x = new QTreeWidgetItem(ui->treeWidget);    
        x->setText(0,ArraySet1[i][0]);
        ui->treeWidget->addTopLevelItem(x);
    
        QTreeWidgetItem *y = new QTreeWidgetItem();
        y->setText(0,ArraySet1[i][1]);
        x->addChild(y);
    
        QTreeWidgetItem *z = new QTreeWidgetItem();
        z->setText(0,ArraySet1[i][2]);
        y->addChild(z);
    }
    

    Thanks in advance!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 7 Jul 2015, 20:58 last edited by
      #2

      you get at tree like this ?

      TopParent_1
        child
          subchild
      TopParent_2
        child
          subchild
      

      and then if you list then have another TopParent_1, you want its child and sub child to go to that one , so you get

      TopParent_1
        child
          subchild
        child
          subchild
      

      If you want to keep track of already seen TopParents, you could use a hash map.
      QHash<QString, QTreeWidgetItem * > Parents;

      That way you can check if you seen the parent text before
      and then add to that one, and if not seen before
      just do as you do now.

      B 1 Reply Last reply 8 Jul 2015, 10:07
      0
      • M mrjj
        7 Jul 2015, 20:58

        you get at tree like this ?

        TopParent_1
          child
            subchild
        TopParent_2
          child
            subchild
        

        and then if you list then have another TopParent_1, you want its child and sub child to go to that one , so you get

        TopParent_1
          child
            subchild
          child
            subchild
        

        If you want to keep track of already seen TopParents, you could use a hash map.
        QHash<QString, QTreeWidgetItem * > Parents;

        That way you can check if you seen the parent text before
        and then add to that one, and if not seen before
        just do as you do now.

        B Offline
        B Offline
        Bart
        wrote on 8 Jul 2015, 10:07 last edited by Bart 7 Aug 2015, 10:33
        #3

        @mrjj

        Hi, thank you for your help.

        I tryed this:

        QTreeWidgetItem *x = new QTreeWidgetItem(ui->treeWidget);
            x>setText(0,ArraySet1[i][0]);
            QHash<QString, QTreeWidgetItem * > parents;
            parents.insert(ArraySet1[i][0], x);
        

        but I don't understand why it shouldn't work. In my understanding with insert it should replace the same values.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bart
          wrote on 8 Jul 2015, 11:14 last edited by Bart 7 Aug 2015, 11:39
          #4

          I changed it now to this but this also doesn't seem to work for some reason

          for(int i = 0; i < s1; i++)
             {
                  QHash<QString, QTreeWidgetItem * > parents;
                  QTreeWidgetItem *x = new QTreeWidgetItem(ui->treeWidget);
          
                  if(blades.contains(ArraySet1[i][0]))
                  {
                      QTreeWidgetItem *y = new QTreeWidgetItem();
                      y>setText(0,ArraySet1[i][1]);
                      x->addChild(y);
          
                     QTreeWidgetItem *z = new QTreeWidgetItem();
                     z->setText(0,ArraySet1[i][2]);
                     y->addChild(z);
                  }
                  else
                  {
                     x->setText(0,ArraySet1[i][0]);
                     ui->treeWidget->addTopLevelItem(x);
                     parents[ArraySet1[i][0]] = x;
          
                      QTreeWidgetItem *y = new QTreeWidgetItem();
                      y>setText(0,ArraySet1[i][1]);
                      x->addChild(y);
          
                     QTreeWidgetItem *z = new QTreeWidgetItem();
                     z->setText(0,ArraySet1[i][2]);
                     y->addChild(z);
                  }
             }
          
          M 1 Reply Last reply 8 Jul 2015, 14:05
          0
          • B Bart
            8 Jul 2015, 11:14

            I changed it now to this but this also doesn't seem to work for some reason

            for(int i = 0; i < s1; i++)
               {
                    QHash<QString, QTreeWidgetItem * > parents;
                    QTreeWidgetItem *x = new QTreeWidgetItem(ui->treeWidget);
            
                    if(blades.contains(ArraySet1[i][0]))
                    {
                        QTreeWidgetItem *y = new QTreeWidgetItem();
                        y>setText(0,ArraySet1[i][1]);
                        x->addChild(y);
            
                       QTreeWidgetItem *z = new QTreeWidgetItem();
                       z->setText(0,ArraySet1[i][2]);
                       y->addChild(z);
                    }
                    else
                    {
                       x->setText(0,ArraySet1[i][0]);
                       ui->treeWidget->addTopLevelItem(x);
                       parents[ArraySet1[i][0]] = x;
            
                        QTreeWidgetItem *y = new QTreeWidgetItem();
                        y>setText(0,ArraySet1[i][1]);
                        x->addChild(y);
            
                       QTreeWidgetItem *z = new QTreeWidgetItem();
                       z->setText(0,ArraySet1[i][2]);
                       y->addChild(z);
                    }
               }
            
            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 8 Jul 2015, 14:05 last edited by mrjj 7 Aug 2015, 14:06
            #5

            @Bart
            Hi,
            please
            put

            QHash<QString, QTreeWidgetItem * > Parents; 
            

            outside the for loop.
            Also,
            you say

             if(blades.contains(ArraySet1[i][0]))
            

            Should that not be Parents?
            Anyway, when Contains is true, you do not create a new QTreeWidgetItem but instead use the one you store in the
            hash list. that way you will connect to the same Parent (the first one) when you see the same name again.
            We use the list to keep track of ParentName, and the QTreeWidgetItem that we created with that name.
            so

             if(blades.contains(ArraySet1[i][0]))
                    {
                        QTreeWidgetItem *y = new QTreeWidgetItem();
                        y>setText(0,ArraySet1[i][1]);
                        x->addChild(y); <----------------- x here should be the parent you stored in the hash list
            }
            

            becomes

             if(Parents.contains(ArraySet1[i][0]))
             {
                        QTreeWidgetItem *NewChild = new QTreeWidgetItem();
                        NewChild>setText(0,ArraySet1[i][1]);
                       QTreeWidgetItem *StoredParent =Parents.value(ArraySet1[i][0]);
                       StoredParent ->addChild(NewChild)
            } else
            {
            // create the new parent and store in list
            QTreeWidgetItem *x = new QTreeWidgetItem(ui->treeWidget);    
                x->setText(0,ArraySet1[i][0]);
               ui->treeWidget->addTopLevelItem(x);
               Parents->Insert( ArraySet1[i][0], x  ); // store our parent under that name
            
                QTreeWidgetItem *y = new QTreeWidgetItem();
                y->setText(0,ArraySet1[i][1]);
                x->addChild(y);
            
                QTreeWidgetItem *z = new QTreeWidgetItem();
                z->setText(0,ArraySet1[i][2]);
                y->addChild(z);
            l
            }
            

            something like that. Hope it makes sense.
            ps. did not try to compile so might be syntax errors.

            1 Reply Last reply
            0

            1/5

            7 Jul 2015, 15:10

            • Login

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