代码之家  ›  专栏  ›  技术社区  ›  Megidd

树状图中的过滤器模型

  •  0
  • Megidd  · 技术社区  · 6 年前

    这是一个后续问题 this one 已经修好了。


    我的模型有一些自定义函数,它们在树状视图中被调用。例如,我的 ItemModel 班级:

    class ItemModel : public QAbstractItemModel
    {
        // ...
    
        Q_INVOKABLE void addExpandedItem(const QModelIndex &index);
        Q_INVOKABLE void removeExpandedItem(const QModelIndex &index);
    }
    

    使用 项目模型 类别as sceneModel ParentClass :

    class ParentClass : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(ItemModel * sceneModel READ sceneModel CONSTANT)
    
    private:
        ItemModel *m_sceneModel;
    
    }
    

    打电话 / 这些自定义功能包括:

    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

    0 回复  |  直到 6 年前
        1
  •  0
  •   user3417815    6 年前

    QVariant 在里面 Q_PROPERTY 你父母班的。Getter函数应该返回类实例,如下所示:

    return QVariant::fromValue(m_sceneModel);
    

    TreeView 通行证 QModelIndex 你的方法?据我所知 int 而实际模型索引只能由C++访问 data 功能。