[SOLVED]how can i remove all rows from QStandardItemModel except for headers ?
-
i have QStandardItemModel and Treeview that get filled dynamically every 10 seconds
before it get filled i need to clear it.
the problem with clear() method is that it clears me also the treeview headers . is there better way ?
dos removeRows will work if does , how can i get the total rows count and how can i set the parent that is the right parent ( the headers row ?)
Thanks -
"removeRows ":http://doc.qt.nokia.com/4.7/qstandarditemmodel.html#removeRows
seem to be suitable -
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);
}
}@ -
[quote]
@ FirstQModelIndex = m_model->item(0,0)->index();
QStandardItem* feedItem = m_model->itemFromIndex(FirstQModelIndex);
@ [/quote]
Why the redundancy?[quote]
@
// get the parent of the first row its the header row
QStandardItem* parentItem = feedItem->parent();
@
[/quote]
Your first item doesn't have a parent.[quote]
@
// here im getting exception
int parent_rows= parentItem->hasChildren();
parentQModelIndex = m_model->indexFromItem(parentItem);
@
[/quote]
parentItem = 0 --> SIGSEGVWhy not just
@
m_model->removeRows(0,m_model.rowCount());
@
or
@
m_model->invisibleRootItem()->removeRows(0,m_model.rowCount());
@