bardao
bardao

Reputation: 954

Using QAbstractItemModel as a Q_PROPERTY inside c++ with READ WRITE

I'm trying to achieve a filter model that has a source model inside QML. Both of my models are c++ based and registered as modules.

Assuming I have:

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

    }
}

The c++ of MyTreeModelCompleter:

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;
};

My main issue is including MyTreeModel inside the completer model MyTreeModelCompleter. The issue is generated when I try to bind them, the compiler complains that the values don't match in types as 1 is QAbstractItemModel* and the other is QAbstractItemModel. Is there a way to get this model binding to work? This is by no means a working code, because I think the issue is in the embedding of the Models in one another and not in the actual code.

The error:

Cannot assign object of type "MyTreeModel" to property of type "QAbstractItemModel*" as the former is neither the same as the latter nor a sub-class of it.

Upvotes: 4

Views: 1089

Answers (1)

tunglt
tunglt

Reputation: 1072

A way to handle the problem is to use QObject * instead of QAbstractItemModel * for the model property. Here is an example :

    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;
    };

Upvotes: 2

Related Questions