1 上移 / 下移
//上移 一个
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1);
mDataList.swapItemAt(row - 1, row);
endMoveRows();
以下官方不推荐隐藏更新手法不推荐操作
emit rowsMoved(QModelIndex(), row, row, QModelIndex(), row, {});
// 下移 一个
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row + 2);
mDataList.swapItemAt(row, row+1);
endMoveRows();
以下官方不推荐隐藏更新手法不推荐操作
emit rowsMoved(QModelIndex(), row+1, row+1, QModelIndex(), row+1, {});
//为何是不是 row+1 而是 row + 2 ,如图所示,需要知道4的这个位置插入才算是上下换位
合并版
bool moveRow(const int& sourceRow, const int& desRow) {
//范围检查
···
//移动
beginMoveRows(QModelIndex(), sourceRow, sourceRow, QModelIndex(),
(desRow > sourceRow) ? (desRow + 1) : desRow);
mRecords -> swapItemsAt(sourceRow, desRow);
endRows();
return true);
}
2 添加 / 删除
beginInsertRows(QModelIndex() , ps, ps);
mRecords->append(temp);
endInsertRows();
beginRemoveRos(QModelIndex(), ps, ps);
mRecords->removeAt(ps);
endRemoveRows();