iHowell
iHowell

Reputation: 2447

How do I interact with a model in an extended QQuickItem?

There are many good resources on dealing with models and views in the Qt doc, like: http://doc.qt.io/qt-5/model-view-programming.html, but I can't seem to find any that link to dealing with models in QtQuick. There are some basic chapters on extending qml with c++, like in http://doc.qt.io/qt-5/qtqml-tutorials-extending-qml-example.html, and on using models: http://doc-snapshots.qt.io/qt5-5.11/qtquick-modelviewsdata-modelview.html, but I can't quite find a way to use an actual model in extended qml.

Currently I have this model:

class LayoutModel : public QAbstractItemModel {
  Q_OBJECT

public:
  explicit LayoutModel(const QString &data, QObject *parent = 0);
  ~LayoutModel();

  QVariant data(const QModelIndex &index, int role) const override;
  Qt::ItemFlags flags(const QModelIndex &index) const override;
  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
  QModelIndex parent(const QModelIndex &index) const override;
  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  int columnCount(const QModelIndex &parent = QModelIndex()) const override;

private:
  void setupModelData(const QStringList &lines, LayoutItem *parent);

  LayoutItem *rootItem;
};

And I'm trying to access it from this view class:

class Layout : public QQuickItem
{
    Q_OBJECT
    Q_PROPERTY(LayoutModel model READ model WRITE setModel NOTIFY modelChanged)

   private:
    LayoutModel & m_model;

   public:
    explicit Layout(QQuickItem * parent = nullptr);

    LayoutModel & model() const;
    void setModel(const LayoutModel & model);

    void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData & value) override;
    void geometryChanged(const QRectF & newGeometry, const QRectF & oldGeometry) override;

   signals:
    void modelChanged();
};

But I can't find a way to actual use the model. I can't even properly set up read and write to the model, since QAbstractItemModels (and models in Qt in general) have their copy constructor deleted to enforce entity singularity. Here's my current broken implementation:

Layout::Layout(QQuickItem * parent) : QQuickItem(parent) {}

LayoutList & Layout::model() const
{
    return m_model;
}

void Layout::setModel(const LayoutList & model)
{
    if (m_model == model)
        return;

    m_model = model;
    emit modelChanged();
}

So, how can I make it so that I can use this extended qml class with my LayoutModel?

Upvotes: 0

Views: 288

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

QObjects do not have a copy constructor, so you must use a pointer:

*.h

class Layout : public QQuickItem
{
    Q_OBJECT
    Q_PROPERTY(LayoutModel *model READ model WRITE setModel NOTIFY modelChanged)

   private:
    LayoutModel *m_model;

   public:
    explicit Layout(QQuickItem * parent = nullptr);

    LayoutModel *model();
    void setModel(LayoutModel * model);
    ...
   signals:
    void modelChanged();
};

*.cpp

...

LayoutModel  *Layout::model() 
{
    return m_model;
}

void Layout::setModel(LayoutModel *model)
{
    if (m_model == model)
        return;
    m_model = model;
    emit modelChanged();
}

Upvotes: 1

Related Questions