在C ++中使用READ WRITE将QAbstractItemModel用作Q_PROPERTY

时间:2019-01-08 00:28:54

标签: qt qml qt5

我正在尝试建立一个在QML内部具有源模型的过滤器模型。我的两个模型都是基于c ++的,并注册为模块。

假设我有:

ListView {
    id: autocomplete
    anchors.top: field.bottom
    model: MyTreeModelCompleter {
        separator: "."
        model: MyTreeModel{}

    }
}

MyTreeModelCompleter的c ++:

class TreeModelCompleter : public QCompleter
{
    Q_OBJECT
    Q_PROPERTY(QString separator READ separator WRITE setSeparator)
    Q_PROPERTY(QAbstractItemModel* model READ model WRITE setModel)

public:
    explicit TreeModelCompleter(QObject *parent = Q_NULLPTR);
    explicit TreeModelCompleter(QAbstractItemModel *model, QObject *parent = Q_NULLPTR);

    QString separator() const;
    QAbstractItemModel* model();
public slots:
    void setSeparator(const QString&);
    void setModel(QAbstractItemModel*);

protected:
    QStringList splitPath(const QString &path) const override;
    QString pathFromIndex(const QModelIndex &index) const override;

private:
    QString m_sep;
    QAbstractItemModel *m_model;
};

MyTreeModel c ++:

class MyTreeModel : public QAbstractItemModel
{
    Q_OBJECT
...
};

MyTreeModel QML:

MyTreeElement {
    property bool check
    property string name: "name1"
    property string description: "desc of name1"

    MyTreeElement {
        property bool check
        property string name: "name2"
        property string description: "desc of name2"
    }
    MyTreeElement {
        property bool check
        property string name: "name3"
        property string description: "desc of name3"

        MyTreeElement {
            property bool check
            property string name: "name 4"
            property string description: "desc of name4"

            MyTreeElement {
                property bool check
                property string name: "name 5"
                property string description: "desc of name5"
            }
        }

    }
}

MyTreeelement:

class MyTreeNode : public QObject
{
    Q_OBJECT
public:
    Q_PROPERTY(QQmlListProperty<MyTreeNode> nodes READ nodes)
    Q_CLASSINFO("DefaultProperty", "nodes")
    MyTreeNode(QObject *parent = Q_NULLPTR);

    void setParentNode(MyTreeNode *parent);
    Q_INVOKABLE MyTreeNode *parentNode() const;
    bool insertNode(MyTreeNode *node, int pos = (-1));
    QQmlListProperty<MyTreeNode> nodes();

    MyTreeNode *childNode(int index) const;
    void clear();

    Q_INVOKABLE int pos() const;
    Q_INVOKABLE int count() const;

private:
    QList<MyTreeNode *> m_nodes;
    MyTreeNode *m_parentNode;
};

我的主要问题是将MyTreeModel包括在完成者模型MyTreeModelCompleter中。当我尝试绑定它们时,会生成问题,编译器会抱怨值的类型不匹配,因为1是QAbstractItemModel*,另一个是QAbstractItemModel。 有没有办法使该模型绑定起作用? 这绝不是一个有效的代码,因为我认为问题在于将模型相互嵌入,而不是实际代码中。

错误:

  

无法将类型“ MyTreeModel”的对象分配给类型的属性   “ QAbstractItemModel *”与前者既不相同   也不是它的子类。

1 个答案:

答案 0 :(得分:0)

解决问题的一种方法是将QObject *属性使用QAbstractItemModel *而不是model。这是一个示例:

    class TreeModelCompleter : public QCompleter
    {
        Q_OBJECT
        Q_PROPERTY(QString separator READ separator WRITE setSeparator)
        Q_PROPERTY(QObject* model READ model WRITE setModel)

    public:
        explicit TreeModelCompleter(QObject *parent = Q_NULLPTR);
        explicit TreeModelCompleter(QAbstractItemModel *model, QObject *parent = Q_NULLPTR);

        QString separator() const;
        QObject* model();
    public slots:
        void setSeparator(const QString&);
        void setModel(QObject*); 

    protected:
        QStringList splitPath(const QString &path) const override;
        QString pathFromIndex(const QModelIndex &index) const override;

    private:
        QString m_sep;
        QAbstractItemModel *m_model;
    };