我有一个 QAbstractListModel 派生的 C++ 类。
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;
};
然后我有一个 BackEnd.cpp,它向 MyList 类对象添加和删除项目
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;
}
在 Main.cpp 我注册 MyList 类型并将后端设置为 qml 根上下文
qmlRegisterType<MyList >("MyList", 1, 0, "MyListModel");
engine->rootContext()->setContextProperty("backend", backend);
在 QML 中我使用过
ListView {
model: MyListModel {
backend: backend
}
}
现在我想对我的列表进行排序,并且已经通过了 QSortFilterProxyModel 但无法弄清楚如何将 QSortFilterProxyModel 与我当前的架构相匹配作为我注册为 qml 类型的模型。