我想删除不可见的根项目的树视图中的所有树项目。
当前,这就是我的工作流程
QModelIndex index = treeView->rootIndex();
QAbstractItemModel *model = treeView->model();
TreeModel *myModel = qobject_cast<TreeModel*>(model);
TreeItem* itm = myModel->getItem(index);
itm->removeChildren(0, itm->childCount());
bool TreeItem::removeChildren(int position, int count)
{
if (position < 0 || position > childItems.count())
return false;
for (int row = 0; row < count; ++row)
{
delete childItems.takeAt(position);
}
return true;
}
尽管我能够删除树视图中的所有项目,但似乎树模型索引没有得到更新。
在删除所有Tree项目后,如果我尝试添加新项目,则应用程序崩溃。
答案 0 :(得分:3)
您需要在这些调用之间删除元素。
beginResetModel();
和
endResetModel()