3

I am trying to build a tree in a way that I can insert the element where I want. This is my tree in its initial stage:

+Project
+--Version 1.0
+--Version 2.0

Now, let's suppose I made a Version 1.1 and I would like to add it at the location beetween Version 1.0 and Version 2.0. Like:

+Project
+--Version 1.0
+--Version 1.1
+--Version 2.0

I have tried to add it using insertChild ( http://doc.qt.digia.com/4.7-snapshot/qtreewidgetitem.html#insertChild ) but the item does not get created in the tree. This is the code:

void VersionGuiElements::createGuiElements(QTreeWidgetItem* projectItem, int idxAfter)
{
    QTreeWidgetItem* versionItem = new QTreeWidgetItem(0, QStringList(QString("Version ") + m_version->getVersionText())) ;

    if(idxAfter == -1)
    {
        projectItem->addChild(versionItem);
    }
    else
    {
        projectItem->insertChild(idxAfter, versionItem);
    }
}

but nothing gets inserted in the tree... any idea what am I doing wrongly?

Ferenc Deak
  • 30,889
  • 14
  • 82
  • 151

1 Answers1

0

You should point out the parent of tree node when you create it:

QTreeWidgetItem* versionItem = new QTreeWidgetItem(projectItem, QStringList(QString("Version ") + m_version->getVersionText())) ;

Then:

if(idxAfter == -1)
{
    projectItem->addChild(versionItem);
}
else
{
    projectItem->insertChild(idxAfter, versionItem);
}
Al2O3
  • 2,695
  • 4
  • 22
  • 46