In the QML 动态视图排序教程 3 https://doc.qt.io/qt-5/qtquick-tutorials-dynamicview-dynamicview3-example.html我替换的例子visualModel.items.move()
打电话给我的ObjectListModel::move()
像这样的方法:
ObjectListModel :公共 QAbstractListModel:
void ObjectListModel::move(int from, int to)
{
if(0 <= from && from < count() && 0 <= to && to < count() && from != to) {
if(from == to - 1) // Allow item moving to the bottom
to = from++;
beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
objectList.move(from, to);
endMoveRows();
}
}
委托组件:
DropArea {
anchors { fill: parent; }
onEntered: {
let from = drag.source.DelegateModel.itemsIndex
let to = mouseArea.DelegateModel.itemsIndex
objectListModel.move(from, to)
}
}
以上非常适合ListView
and ObjectListModel
本身 - 我已经检查过:项目(以及对象)移动正确,索引正确,C++消费者工作正常并正确考虑新订单,等等。
然而另一位消费者喜欢MapItemView
之后无法使用该模型beginMoveRows
/endMoveRows
调用:移动的项目在地图上消失,并且对项目的其他操作会使应用程序崩溃。
Map {
...
MapItemView {
model: objectListModel
delegate: SomeItemIndicator {
}
}
}
已报道QTBUG-81076 https://bugreports.qt.io/browse/QTBUG-81076错误,已确认。
解决方法:
目前找到解决方法:创建第二个重复模型,每次添加/删除/移动(重新排序)时,第一个模型中的每次更改都会完全替换内容。以上作品自beginResetModel
/endResetModel
正确适用于MapItemView
. So MapItemView
现在仅使用第二种模型。因此,每次第一个模型更改时,第二个模型都会调用此方法:
QObjectList ObjectListModel::swapObjectList(const QObjectList& newlist)
{
QObjectList oldlist(_objectList);
beginResetModel();
_objectList = newlist;
endResetModel();
return oldlist;
}