PDA

View Full Version : How to remove all rows and chiled rows from QTreeview



umen
14th August 2011, 12:23
Hi
i dont know why im get into trouble with removing all rows and sub rows from qtreeview
im using QStandardItemModel as the model . now here is my code that doesn't work .



QModelIndex FirstQModelIndex;
QModelIndex parentQModelIndex;
int iMdlChidCound = m_model->hasChildren();
if(iMdlChidCound > 0)
{
// only if there at list 1 row in the view
FirstQModelIndex = m_model->item(0,0)->index();
QStandardItem* feedItem = m_model->itemFromIndex(FirstQModelIndex);
// get the parent of the first row its the header row
QStandardItem* parentItem = feedItem->parent();


// here im getting exception
int parent_rows= parentItem->hasChildren();
parentQModelIndex = m_model->indexFromItem(parentItem);


// now i like to delete all the rows under the header , and its dosnt work
if(parent_rows>0)
{
bool b = feedItem->model()->removeRows(0,y,parentQModelIndex);
}
}

d_stranz
14th August 2011, 17:51
You don't bother to check that "parentItem" is non-NULL, so of course it will throw if parentItem == 0.

norobro
15th August 2011, 00:26
. . . removing all rows and sub rows from qtreeview . . .


Why not do this?
m_model->removeRows(0, m_model->rowCount());