如何从QTreeview中删除所有行和子行 [英] How to remove all rows and child rows from QTreeview

查看:3526
本文介绍了如何从QTreeview中删除所有行和子行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我无法从qtreeview中删除所有行和子行

I don't know why I have trouble removing all rows and sub rows from qtreeview

我使用QStandardItemModel作为模型。现在这里是我的代码不工作。

I'm 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);
    }
}


推荐答案

似乎很多你在做什么是多余的。如果您唯一的目标是从模型中删除所有行,则可以使用 QStandardItemModel :: clear

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.

这篇关于如何从QTreeview中删除所有行和子行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆