0

I want to make it so that I can expand a third level (subchild) to a child under the top level item (root). All I've been able to do is make multiple children to a single root.

this is in my .cpp

    QStringList string1, string2;
    string1 << "xxxxxxxx" << "xxxxxxxxxxx";
    string2 << "yyyyyy" << "yy";

    m_treeWidget->insertTopLevelItem(0, new QTreeWidgetItem(string1));
    m_treeWidget->insertTopLevelItem(1, new QTreeWidgetItem(string2));


    //here I add a child
    AddChild(m_treeWidget->topLevelItem(0),"hello","world", m_treeWidget);

    //here I make two attempts to make a sub child
    AddChild(m_treeWidget->itemBelow(m_treeWidget->topLevelItem(0)),"hello_sub1","world_sub1", m_treeWidget);
    AddChild(m_treeWidget->itemAt(0,0),"hello_sub2","world_sub2", m_treeWidget);

The following is my Add Child Method also in the same .cpp file:

    void Dialog::AddChild (QTreeWidgetItem *parent, QString name, QString Description, QTreeWidget* treeWidget)
    {
         QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
         item->setText(0,name);
         item->setText(1, Description);
         parent->addChild(item);
    }
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
EricaAKrause
  • 11
  • 1
  • 2

2 Answers2

1

In order to make a tree hierarchy you can use the QTreeWidgetItem's API, especially its constructors. Constructors can accept either QTreeWidget or QTreeWidgetItem as a parent object. In the first case, the top level item will be added to the tree widget and in the second case - child item of another item. This API is easier to use because you don't need to explicitly append items to the tree widget. Here is the sample code that implements the idea:

QStringList string1, string2;
string1 << "xxxxxxxx" << "xxxxxxxxxxx";
string2 << "yyyyyy" << "yy";

QTreeWidget tv;

// The top level items
QTreeWidgetItem *top1 = new QTreeWidgetItem(&tv, string1);
QTreeWidgetItem *top2 = new QTreeWidgetItem(&tv, string2);

// A child item.
QTreeWidgetItem *child1 =
               new QTreeWidgetItem(top1, QStringList() << "Hello" << "World");

// The grandchildren.
new QTreeWidgetItem(child1, QStringList() << "Hello_sub1" << "World_sub1");
new QTreeWidgetItem(child1, QStringList() << "Hello_sub2" << "World_sub2");
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Thanks, it took me a minute to find which constructors from the API that you were using, but here they are (for any reader's future reference): QTreeWidgetItem ( QTreeWidget * parent, int type = Type ) QTreeWidgetItem ( QTreeWidgetItem * parent, int type = Type ) – EricaAKrause Jul 15 '14 at 13:47
  • But I had to take away the & signs. Why did you use those? – EricaAKrause Jul 15 '14 at 17:41
  • Because in my example tv is a value, not a pointer. I had to take address of it. – vahancho Jul 15 '14 at 19:25
0

Actually I was able to solve it another way...

in the .cpp:

    //Initialize the QTreeWidget with 2 columns

    QTreeWidget m_treeWidget = new QTreeWidget();
    m_treeWidget->setColumnCount(2);

    //these are the method calls:

    AddRoot("Root1_Column1", "Root2_Column2", m_treeWidget);
    AddRoot("Root2_Column1", "Root2_Column2", m_treeWidget);

    //topLevelItem(0) makes it a child of the first root... topLevelItem(1) makes it a child of the second root
    AddChild(m_treeWidget->topLevelItem(0),"Child1_Column1","Child1_Column2"); 
    AddChild(m_treeWidget->topLevelItem(1),"Child2_Column1","Child2_Column2");

    AddSubChild(m_treeWidget->itemBelow(m_treeWidget->topLevelItem(0)),"SubChild_Column1", "SubChild_Column2");

With these being the methods I used within the same .cpp file:

    void Dialog::AddRoot (QString name, QString Description, QTreeWidget* treeWidget)
    {
       QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
       item->setText(0,name);
       item->setText(1,Description);
       item->setExpanded(true); //expand automatically
       treeWidget->addTopLevelItem(item);
    }

    void Dialog::AddChild (QTreeWidgetItem *parent, QString name, QString Description)
    {
       QTreeWidgetItem *item = new QTreeWidgetItem();
       item->setText(0,name);
       item->setText(1, Description);
       parent->addChild(item);
    }

    void Dialog::AddSubChild (QTreeWidgetItem *parent, QString name, QString Description)
    {
       QTreeWidgetItem *item = new QTreeWidgetItem();
       item->setText(0,name);
       item->setText(1, Description);
       parent->addChild(item);
    }
EricaAKrause
  • 11
  • 1
  • 2
  • This is no good coding style as you have a lot of duplicate code. AddChild and AddSubChild are even identical (logical and in your code). – jaba Oct 11 '16 at 16:14