如何从QTreeview中删除所有行和子行
How to remove all rows and child rows from QTreeview
我不知道为什么我有麻烦从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::clear
在你的代码中,你以一种你不必的方式在模型和项目之间跳转。
if(m_model->hasChildren()) {
m_model->removeRows(0, m_model->rowCount());
}
应该能满足你的要求。
QStandardItemModel::clear()
清除包括标题行在内的所有项
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 删除指向指针的指针是运行时错误吗
- 从 QTreeView 中删除项目时取消选择所有行
- 使用自定义上下文菜单删除 QTreeView 项崩溃
- 使用自定义模型从Qtreeview删除行
- 在QTreeView中插入和删除行
- QTreeView-删除某些元素的可扩展外观
- QTreeView with QFileSystemModel:如何删除除"Name"之外的所有列?
- 如何从QTreeview中删除所有行和子行