PDA

View Full Version : Move items in a QSortFilterProxyModel



volcano
18th July 2018, 09:30
I have a tableview with takes a qsortfilterproxymodel and is filtered by a particular name.

I have subclassed the qsortfilterproxymodel

class FilterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
FilterProxyModel(QObject *parent = 0);

void moveUp( const int itemIndex);
void moveDown( const int itemIndex);
};

and here's the implementation

FilterProxyModel ::FilterProxyModel (QObject *parent) :
QSortFilterProxyModel(parent)
{

}

void FilterProxyModel ::moveUp(const int itemIndex)
{
if(itemIndex > 0 && itemIndex < rowCount())
{
beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(),
itemIndex - 1);
moveRow(QModelIndex(), itemIndex, QModelIndex(), itemIndex - 1);
endMoveRows();
}
}

void FilterProxyModel ::moveDown(const int itemIndex)
{
if(itemIndex >= 0 && itemIndex < rowCount() - 1)
{
beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(),
itemIndex + 2);
moveRow(QModelIndex(), itemIndex, QModelIndex(), itemIndex + 2);
endMoveRows();
}
}


Here's how the model implemented

m_Model = new FilterProxyModel(this);
m_Model ->setSourceModel(partiesModel);
m_Model ->setFilterRegExp(QRegExp("party", Qt::CaseInsensitive, QRegExp::FixedString));
m_Model ->setFilterKeyColumn(PartyModel::Action);

I want to move the items using a push button, when i call the function the move doesn't happen. Could you tell me what I'm missing?

tuli
19th July 2018, 13:05
Check return value of moveRow() and shows that function.

volcano
20th July 2018, 09:48
I will write a custom implementation of the move and try