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).
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
}
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 !
treeWidget->indexOfTopLevelItem(item) > -1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With