开发者

How to remove all rows and child rows from QTreeview

I don't know why I have trouble removing all rows and sub rows from qt开发者_JAVA技巧reeview

I'm using QStandardItemModel as the model. Now here is my code that doesn't work.

What could be the problem?

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);
    }
}


It seems like a lot of what you're doing is superfluous. If your only goal is to remove all the rows from the model, you could probably just use QStandardItemModel::clear

In your code you're jumping between the model and the items in a way you don't have to.

if(m_model->hasChildren()) {
    m_model->removeRows(0, m_model->rowCount());
}

That should do what you're seeking.


QStandardItemModel::clear()

Which clears all the items including the header rows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜