Sort a QAbstractListModel with backend list

283 Views Asked by At

I have a QAbstractListModel-derived C++ class.

class MyList : public QAbstractListModel
{
    Q_OBJECT
public:
    MyList();
 enum {
        SelectedRole,
        DisplayNameRole,
        AddressRole
};

     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;

    bool setData(const QModelIndex& index, const QVariant& value,
        int role = Qt::EditRole) override;

    private:
    BackEnd backend;
};

Then I have a BackEnd.cpp which adds and removes items to the MyList class object

class BackEnd : public QObject {
        // adds item of type MyList to mItems
       bool setItemAt(int index, const Item& item);
    
    private:
    // this has items of type MyList. (i.e QAbstractListModel)
        QVector<Item> mItems;
    }

In the Main.cpp I register the MyList type and set the backend to qml root context

 qmlRegisterType<MyList >("MyList", 1, 0, "MyListModel");
 engine->rootContext()->setContextProperty("backend", backend);

and in QML I have used

ListView {
   model: MyListModel {
        backend: backend
    }
}

Now I want to sort my list my list and and have gone through QSortFilterProxyModel but not able to figure out how to fit QSortFilterProxyModel with my current architecture as my model I have registered as qml type.

0

There are 0 best solutions below