Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]how can i remove all rows from QStandardItemModel except for headers ?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 14.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    umen242
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      "removeRows ":http://doc.qt.nokia.com/4.7/qstandarditemmodel.html#removeRows
      seem to be suitable

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • U Offline
        U Offline
        umen242
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on last edited by
          #4

          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().

          1 Reply Last reply
          0
          • U Offline
            U Offline
            umen242
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • L Offline
              L Offline
              loladiro
              wrote on last edited by
              #6

              [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());
              @

              1 Reply Last reply
              0
              • U Offline
                U Offline
                umen242
                wrote on last edited by
                #7

                Thanks , working great

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved