Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find if QTreeWidgetItem is top level

Tags:

c++

qt

Is there a way I can find out if the QTreeWidgetItem I'm looking at is top level or not? I have a program crashing when I try to take the text of a parent if the item is top level (no parent).

like image 275
eyecreate Avatar asked Feb 23 '10 18:02

eyecreate


3 Answers

Quoting the documentation:

The main difference between top-level items and those in lower levels of the tree is that a top-level item has no parent(). This information can be used to tell the difference between items, and is useful to know when inserting and removing items from the tree.

if (!node.parent()) {
    // top-level item
}
like image 125
Georg Schölly Avatar answered Nov 09 '22 08:11

Georg Schölly


I would suggest that you could check if the QTreeWidgetItem's parent is NULL.

const int FIRST_NODE_INDEX = 0;
QTreeWidget* pTreeWidget = new QTreeWidget(this);
pTreeWidget->setColumnCount(1);
QList<QTreeWidgetItem *> ItemList;

for (int i = 0; i < 10; ++i)
{
    ItemList.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
}    
pTreeWidget->insertTopLevelItems(0, ItemList);

if(!ItemList.at(FIRST_NODE_INDEX)->parent())
{
    qDebug() << "is TopLevel";
}

Hope it helps !

like image 38
Andy M Avatar answered Nov 09 '22 08:11

Andy M


treeWidget->indexOfTopLevelItem(item) > -1
like image 2
vemod Avatar answered Nov 09 '22 08:11

vemod