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. Moving rows in QAbstractListModel subclasses

Moving rows in QAbstractListModel subclasses

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 3.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.
  • A Offline
    A Offline
    acchariBalaka
    wrote on last edited by
    #1

    I have a class by name GPageModel which derives from QAbstractListModel. My class should support two functions moveUp and moveDown which take index of the item in the model to be moved up and down respectively.

    @
    void GPageModel::moveUp(const int index)
    {
    if(index>0 && index<m_pageList.count())
    {
    beginMoveRows(QModelIndex(),index,index,QModelIndex(),index-1);
    endMoveRows();
    }
    }

    void GPageModel::moveDown(const int index)
    {
    if(index>=0 && index<m_pageList.count()-1)
    {
    beginMoveRows(QModelIndex(),index,index,QModelIndex(),index+1);
    endMoveRows();
    }
    }
    @

    I am using a QList to store the internal data. While the moveUp() function does nothing, call to moveDown() function results in my app crashing. I am new to model\view programming and I couldn't find a good example to demonstrate moving list items using beginMoveRows() and endMoveRows(). How do I move items in a list model ?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      acchariBalaka
      wrote on last edited by
      #2

      Found the solution.
      @
      void GPageModel::moveUp(const int index)
      {
      if(index>0 && index<m_pageList.count())
      {
      beginMoveRows(QModelIndex(),index,index,QModelIndex(),index-1);
      m_pageList.swap(index,index-1);
      endMoveRows();
      }
      }

      void GPageModel::moveDown(const int index)
      {
      if(index>=0 && index<m_pageList.count()-1)
      {
      moveUp(index+1);
      }
      }
      @

      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