这是已解决的this one的后续问题。
我的模型有一些自定义函数,这些函数在树视图中被调用。例如,我的ItemModel
类中有两个自定义函数:
class ItemModel : public QAbstractItemModel
{
// ...
Q_INVOKABLE void addExpandedItem(const QModelIndex &index);
Q_INVOKABLE void removeExpandedItem(const QModelIndex &index);
}
将ItemModel
类用作sceneModel
中的ParentClass
属性:
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
}
}
[现在,当我打算按照QSortFilterProxyModel
中所述通过original question过滤模型时,我无法通过QSortFilterProxyModel
代理模型调用那些自定义函数。我收到这样的错误:
qrc:/.../...Tree.qml:191: TypeError: Property 'addExpandedItem' of object QSortFilterProxyModel(0x1f197c082c0) is not a function
因此,我想知道如何通过QSortFilterProxyModel
代理模型调用模型的自定义函数。
在您的父类的QVariant
中将其更改为Q_PROPERTY
。 Getter函数应返回您的类实例,如下所示:
return QVariant::fromValue(m_sceneModel);
当我这样做时,它会起作用。另外,您确定TreeView
将QModelIndex
传递给您的方法吗?据我所知,它仅传递int
,而实际模型索引只能由C ++ data
函数访问。