QAbstractItemModel::rowsMoved problem when moving a row to the last in the same parent
-
Env
- Qt 6.2.1
- Linux
- KDE
Problem
The problem only occurs when move rows in the same parent, only one row moving is tested
e.g. inQTreeView
- 5 rows in one parent
- when moving index 0 to index 2 (.i.e. before index 2), it will exchange index 0 and 1, it should call
beginMoveRows(parent, 0, 0, parent, 2)
. If callingbeginMoveRows(parent, 0, 0, parent, 1)
,endMoveRows
fails. - when moving index 0 to the last (.i.e after index 5), if following the rule of step 2, it should call
beginMoveRows(parent, 0, 0, parent, 5)
, butrowsMoved
signal handler fails, because index 5 is out of range andmodel()->index(row, 0, destination)
returns an invalid model index
Question
When moving a row in the same parent, what's the proper way to make it work for both
- moving a row one row down
- moving a row to the last
Is the way in step 2 right?
If it is right,row
fordestination
should be adjusted manually inrowsMoved
signal handler when moving a row to the last in the same parent? -
@jronald said in QAbstractItemModel::rowsMoved problem when moving a row to the last in the same parent:
which is out of range, is it as expected?
Yes, it's correct. If the start is before the end and you want to know where it is now you have to do some basic math.
rowsMoved() simply informs you about what was given to beginMoveRows() -
QAbstractItemModel is correct, please provide a minimal, compilable example.
-
QAbstractItemModel is correct, please provide a minimal, compilable example.
@Christian-Ehrlicher said in QAbstractItemModel::rowsMoved problem when moving a row to the last in the same parent:
QAbstractItemModel is correct, please provide a minimal, compilable example.
It takes a lot time to write a sample for this.
To simplify the question,
e.g.- In QTreeView, within one parent, there are 5 items
- Drag and drop the 1st item to the last in the same parent
- In
rowsMoved
signal handler as below, paramrow
is 5, which is out of range, is it as expected?void QAbstractItemModel::rowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row)
-
@jronald said in QAbstractItemModel::rowsMoved problem when moving a row to the last in the same parent:
which is out of range, is it as expected?
Yes, it's correct. If the start is before the end and you want to know where it is now you have to do some basic math.
rowsMoved() simply informs you about what was given to beginMoveRows()