QML:原始模型中的动态视图重新排序 [英] QML: Dynamic view re-ordering in original model

查看:170
本文介绍了QML:原始模型中的动态视图重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此Qt教程通过拖动视图项来实现QML动态视图排序: QML动态视图订购教程。在我们的案例中,原始基础模型是 QAbstractListModel 子孙。模型将数据存储在 QList< QObject *>中。 objectList; 字段类型。工作正常,但是仅在代理 DelegateModel 中更改了项目顺序。

Implemented QML Dynamic View Ordering by Dragging View Items using this Qt tutorial: QML Dynamic View Ordering Tutorial. Original underlying model is QAbstractListModel descendant in our case. Model stores data in a QList<QObject*> objectList; field type. Works fine, however item ordering changed in proxy DelegateModel only.

如何在原始基础模型中以及在其他重要的C ++和QML使用者中自动更改项目订单?还是我可以以某种方式通过C ++访问某些结果(排序的)列表模型模型?

How to change items order automatically in original underlying model as well for other C++ and QML consumers where order matters? Or I could we otherway access some resulted (sorted) List Model model from C++ somehow?

谢谢您的帮助!

推荐答案

QML动态视图订购教程3 示例 visualModel.items.move()用我的 ObjectListModel :: move()方法调用:

In the QML Dynamic View Ordering Tutorial 3 example I've replaced visualModel.items.move() call with my ObjectListModel::move() method like this:

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 ++使用者工作正常且正确考虑新订单,等等。

And above works perfectly for the ListView and ObjectListModel itself - I have checked: items (and therefore objects) are moved correctly, indexes are correct, C++ consumers works just fine and take new order into account correctly, etc.

但是另一个消费者,例如 MapItemView beginMoveRows / endMoveRows 调用后,$ c>无法使用该模型:移动的项目在地图上消失,并且其他操作一项使应用程序崩溃。

However another consumer like MapItemView fails to use the model after beginMoveRows/endMoveRows calls: moved item disappeared on the map and other manipulations with an item crashes the app.

 Map {
     ...
     MapItemView {
         model: objectListModel

         delegate: SomeItemIndicator {
         }
     }
 }

已报告 QTBUG-81076 错误,已确认。

Reported QTBUG-81076 bug, which is confirmed.

解决方法:

现在找到解决方法:创建了第二个重复模型,其内容在第一个模型的每次更改,每次添加/删除/移动(重新排序)时,将完全替换。由于 beginResetModel / endResetModel 对于 MapItemView 正常工作,上述方法有效。因此, MapItemView 现在仅使用第二个模型。因此,在每次第一个模型更改时,都会为第二个模型调用此方法:

Found workaround for now: created 2nd duplicate model which content will be replaced completely on every change in the 1st model on every add/delete/moving(reordering). Above works since beginResetModel/endResetModel works correctly for MapItemView. So MapItemView now utilizes only the 2nd model. So on every 1st model change this method is called for the 2nd model:

QObjectList ObjectListModel::swapObjectList(const QObjectList& newlist)
{
    QObjectList oldlist(_objectList);
    beginResetModel();
    _objectList = newlist;
    endResetModel();
    return oldlist;
}

这篇关于QML:原始模型中的动态视图重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆