我不知道为什么我无法从 qtreeview 中删除所有行和子行
我使用 QStandardItemModel 作为模型。现在这是我的代码不起作用。
可能是什么问题呢?
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);
}
}
看来你所做的很多事情都是多余的。如果您的唯一目标是从模型中删除所有行,您可能可以使用QStandardItemModel::清除 http://doc.qt.io/archives/qt-4.8/qstandarditemmodel.html#clear
在您的代码中,您以不必要的方式在模型和项目之间跳转。
if(m_model->hasChildren()) {
m_model->removeRows(0, m_model->rowCount());
}
那应该可以满足您的要求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)