【发布时间】:2020-03-26 10:31:12
【问题描述】:
这是this one 的后续问题,已修复。
我的模型有一些自定义函数,它们在我的树视图中被调用。例如,我的ItemModel 类中有两个自定义函数:
class ItemModel : public QAbstractItemModel
{
// ...
Q_INVOKABLE void addExpandedItem(const QModelIndex &index);
Q_INVOKABLE void removeExpandedItem(const QModelIndex &index);
}
在ParentClass 中使用ItemModel 类作为sceneModel 属性:
class ParentClass : public QObject
{
Q_OBJECT
Q_PROPERTY(ItemModel * sceneModel READ sceneModel CONSTANT)
private:
ItemModel *m_sceneModel;
}
在我的 QML 树视图中,我正在调用/调用这些自定义函数,例如:
TreeView {
model: parentClass.sceneModel
selection: ItemSelectionModel {
model: parentClass.sceneModel
}
onExpanded: {
model.addExpandedItem(index) // Calling custom function
}
onCollapsed: {
model.removeExpandedItem(index) // Calling custom function
}
}
现在,当我打算按照original question 中的描述通过QSortFilterProxyModel 过滤我的模型时,我无法通过QSortFilterProxyModel 代理模型调用这些自定义函数。我收到这样的错误:
qrc:/.../...Tree.qml:191: TypeError: Property 'addExpandedItem' of object QSortFilterProxyModel(0x1f197c082c0) is not a function
因此,我想知道如何通过QSortFilterProxyModel 代理模型调用我的模型的自定义函数。
【问题讨论】:
-
我建议您通过所有 QML 侧过滤。这个非常方便的库github.com/oKcerG/SortFilterProxyModel。这样,您可以在原始模型上调用 addExpandedItem/removeExpandedItem 广告,SortFilterProxyModel 将透明地使用这些新项目进行更新。或者,您可以将
sortedFilteredModel之类的属性公开为原始模型的 Q_PROPERTY 并将您的原始模型公开给 QML,但在您的视图中使用model: originalModel.sortedFilteredModel。
标签: c++ qt qml qtreeview qabstractitemmodel