PDA

View Full Version : beginMoveRows working down to up but not up to down... Any insignt would be great.



hickscorp
3rd August 2011, 20:26
Here are two methods from my model. The first one (Moving a row up) is working, while the 2nd to do the opposite and only slightly different doesn't. What could be wrong with my code here?

bool REA::ZoneRecognizersModel::moveUp (quint32 row, QModelIndex const &parent) {
TreeItem *pItm = (TreeItem*)parent.internalPointer();
if (!pItm) pItm = _root;
if (row<=0 || row>=pItm->children.size())
return false;
quint32 dstRow = row-1;
beginMoveRows (parent, row, row, parent, dstRow);
pItm->children.swap (row, dstRow);
endMoveRows ();
return true;
}
bool REA::ZoneRecognizersModel::moveDown (quint32 row, QModelIndex const &parent) {
TreeItem *pItm = (TreeItem*)parent.internalPointer();
if (!pItm) pItm = _root;
if (row<0 || row>=pItm->children.size()-1)
return false;
quint32 dstRow = row+1;
beginMoveRows (parent, row, row, parent, dstRow);
pItm->children.swap (dstRow, row);
endMoveRows ();
return true;
}

The error i get is a debugging assert in QStack: !this->empty. If i ignore it, i end up with an invalid allocation size in the QList::detach holding my internal proxy data tree to the model... The error is happening in the endMoveRow method.

It is likely that i'm coding for way too long now, and it could be a very small and silly mistake...

Thanks,
Pierre.

Santosh Reddy
4th August 2011, 04:02
When moving rows down in the same parent (sourceParent and destinationParent are equal), the rows will be placed before the destinationChild index

bool REA::ZoneRecognizersModel::moveUp (quint32 row, QModelIndex const &parent) {
...
beginMoveRows (parent, row, row, parent, row);
...
}
bool REA::ZoneRecognizersModel::moveDown (quint32 row, QModelIndex const &parent) {
...
beginMoveRows (parent, row, row, parent, (row >= pItm->children.size()) ? pItm->children.size() : (row + 2));
...
}