[ACCEPTED]-If I add an item to QTreeWidget it always gets inserted in first row-qt

Accepted answer
Score: 10

Try this way:

If you want to add a top level 3 item with children:

//you could also create it dinamically
QTreeWidget * tree = ui->treeWidget;

QTreeWidgetItem * topLevel = new QTreeWidgetItem();
topLevel->setText(0, "This is top level");

for(int i=0; i<5; i++)
{
    QTreeWidgetItem * item = new QTreeWidgetItem();
    item->setText(0,"item " + QString::number(i+1));
    topLevel->addChild(item);
}

tree->addTopLevelItem(topLevel);

Or if you want to add 2 more top-levels:

for(int i=0; i<5; i++)
{
    QTreeWidgetItem * item = new QTreeWidgetItem();
    item->setText(0,"top-level " + QString::number(i+1));
    tree->addTopLevelItem(item);
}

Note, that they are in the 1 order, they got added!

More Related questions