[SOLVED]how can i remove all rows from QStandardItemModel except for headers ?



  • i have QStandardItemModel and Treeview that get filled dynamically every 10 seconds
    before it get filled i need to clear it.
    the problem with clear() method is that it clears me also the treeview headers . is there better way ?
    dos removeRows will work if does , how can i get the total rows count and how can i set the parent that is the right parent ( the headers row ?)
    Thanks


  • Moderators



  • how can get the parent parameter of the QStandardItemModel ? to use in the removeRows.
    also how to get the total rows of the model?



  • The default is to delete the toplevel items. If you need anything else you'll have to obtain the ModelIndex for the parent item (see the respective functions in QStandardItemModel). You can obtain the number of rows by using rowCount().



  • i dont know why im get into trouble with removing all rows and sub rows from qtreeview
    im 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);
    }
    }@



  • [quote]
    @ FirstQModelIndex = m_model->item(0,0)->index();
    QStandardItem* feedItem = m_model->itemFromIndex(FirstQModelIndex);
    @ [/quote]
    Why the redundancy?

    [quote]
    @
    // get the parent of the first row its the header row
    QStandardItem* parentItem = feedItem->parent();
    @
    [/quote]
    Your first item doesn't have a parent.

    [quote]
    @
    // here im getting exception
    int parent_rows= parentItem->hasChildren();
    parentQModelIndex = m_model->indexFromItem(parentItem);
    @
    [/quote]
    parentItem = 0 --> SIGSEGV

    Why not just
    @
    m_model->removeRows(0,m_model.rowCount());
    @
    or
    @
    m_model->invisibleRootItem()->removeRows(0,m_model.rowCount());
    @



  • Thanks , working great


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.