Moving rows in QAbstractListModel subclasses
-
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 ?
-
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);
}
}
@