Qtreewidget prevent duplicate parents



  • 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!


  • Qt Champions 2016

    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.



  • @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.



  • 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);
            }
       }

  • Qt Champions 2016

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.