Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I add an item to QTreeWidget it always gets inserted in first row

Tags:

c++

qt

I want the new items to be inserted after the last row instead of first row.

for(i=0;i<10;i++)
{
    QTreeWidgetItem* tempItem = new QTreeWidgetItem();
    tempItem->setText(0,QString::number(i));
    ui->treeWidget->insertTopLevelItem(tempItem,i);
}

It inserts the items at top, not at the bottom.

like image 699
adnan kamili Avatar asked Jan 22 '26 05:01

adnan kamili


1 Answers

Try this way:

If you want to add a top level 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 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 order, they got added!

like image 192
Balázs Édes Avatar answered Jan 24 '26 21:01

Balázs Édes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!