2011-08-14 10 views
2

からすべての行と子行を削除します。今ここで私のコードは動作しません。は、どのように私はトラブル私はモデルとしてQStandardItemModelを使用していqtreeview</p> <p>からすべての行とサブ行の削除を持っている理由私にはわからないQTreeview

何が問題なのですか?

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

答えて

8

あなたがやっていることの多くは余計なようです。あなたの唯一の目標は、モデルからすべての行を削除する場合は、おそらくQStandardItemModel::clearを使用することができます。

コードでは、モデルと項目の間をジャンプする必要はありません。

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

これは、あなたが探していることを行うはずです。

+0

トップレベルアイテムの場合、親アイテムはnullであるため、例外が発生します。 – Raiv

1

QStandardItemModel ::クリア()

ヘッダ行を含むすべての項目をクリアします。

関連する問題