QML:原始模型中的动态视图重新排序
QML: Dynamic view re-ordering in original model
使用此 Qt 教程通过拖动视图项实现了 QML 动态视图排序:QML Dynamic View Ordering Tutorial。在我们的案例中,原始基础模型是 QAbstractListModel
个后代。模型将数据存储在 QList<QObject*> objectList;
字段类型中。工作正常,但仅在代理 DelegateModel
中更改了项目排序。
如何自动更改原始基础模型中的项目顺序以及其他顺序重要的 C++ 和 QML 消费者?或者我可以以其他方式从 C++ 以某种方式访问一些结果(排序的)List Model 模型吗?
感谢您的帮助!
在 QML Dynamic View Ordering Tutorial 3 示例中,我已将 visualModel.items.move()
调用替换为我的 ObjectListModel::move()
方法,如下所示:
ObjectListModel : public 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
和 ObjectListModel
本身非常有效 - 我已经检查过:项目(因此对象)移动正确,索引正确,C++ 消费者工作正常并采用新的正确考虑顺序等
然而,在 beginMoveRows
/endMoveRows
调用后,另一个像 MapItemView
这样的消费者无法使用该模型:移动的项目在地图上消失,并且对项目的其他操作导致应用程序崩溃。
Map {
...
MapItemView {
model: objectListModel
delegate: SomeItemIndicator {
}
}
}
报告QTBUG-81076错误,已确认。
解决方法:
目前已找到解决方法:创建了第二个重复模型,其内容将在每次 add/delete/moving(重新排序)时完全替换第一个模型的每次更改。以上工作因为 beginResetModel
/endResetModel
对 MapItemView
正确工作。所以 MapItemView
现在只使用第二个模型。因此,在每次第一个模型更改时,都会为第二个模型调用此方法:
QObjectList ObjectListModel::swapObjectList(const QObjectList& newlist)
{
QObjectList oldlist(_objectList);
beginResetModel();
_objectList = newlist;
endResetModel();
return oldlist;
}
使用此 Qt 教程通过拖动视图项实现了 QML 动态视图排序:QML Dynamic View Ordering Tutorial。在我们的案例中,原始基础模型是 QAbstractListModel
个后代。模型将数据存储在 QList<QObject*> objectList;
字段类型中。工作正常,但仅在代理 DelegateModel
中更改了项目排序。
如何自动更改原始基础模型中的项目顺序以及其他顺序重要的 C++ 和 QML 消费者?或者我可以以其他方式从 C++ 以某种方式访问一些结果(排序的)List Model 模型吗?
感谢您的帮助!
在 QML Dynamic View Ordering Tutorial 3 示例中,我已将 visualModel.items.move()
调用替换为我的 ObjectListModel::move()
方法,如下所示:
ObjectListModel : public 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
和 ObjectListModel
本身非常有效 - 我已经检查过:项目(因此对象)移动正确,索引正确,C++ 消费者工作正常并采用新的正确考虑顺序等
然而,在 beginMoveRows
/endMoveRows
调用后,另一个像 MapItemView
这样的消费者无法使用该模型:移动的项目在地图上消失,并且对项目的其他操作导致应用程序崩溃。
Map {
...
MapItemView {
model: objectListModel
delegate: SomeItemIndicator {
}
}
}
报告QTBUG-81076错误,已确认。
解决方法:
目前已找到解决方法:创建了第二个重复模型,其内容将在每次 add/delete/moving(重新排序)时完全替换第一个模型的每次更改。以上工作因为 beginResetModel
/endResetModel
对 MapItemView
正确工作。所以 MapItemView
现在只使用第二个模型。因此,在每次第一个模型更改时,都会为第二个模型调用此方法:
QObjectList ObjectListModel::swapObjectList(const QObjectList& newlist)
{
QObjectList oldlist(_objectList);
beginResetModel();
_objectList = newlist;
endResetModel();
return oldlist;
}