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'm trying to take the text a parent if the item is top level(no parent).
A:
Would probably be best to do it in the model you using behind the treeview
Martin Beckett
2010-02-23 18:57:29
He is probably not using a custom model if he is using a `QTreeWidget`.
Ton van den Heuvel
2010-02-23 20:23:15
+4
A:
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
}
Georg
2010-02-23 19:05:26
+2
A:
Hey,
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 !
Andy M
2010-02-23 19:14:04