【问题标题】:How do I interact with a model in an extended QQuickItem?如何与扩展 QQuickItem 中的模型交互?
【发布时间】:2018-08-06 13:27:29
【问题描述】:

Qt 文档中有很多关于处理模型和视图的好资源,例如:http://doc.qt.io/qt-5/model-view-programming.html,但我似乎找不到任何与在 QtQuick 中处理模型的链接。有一些关于使用 c++ 扩展 qml 的基本章节,例如 http://doc.qt.io/qt-5/qtqml-tutorials-extending-qml-example.html,以及使用模型:http://doc-snapshots.qt.io/qt5-5.11/qtquick-modelviewsdata-modelview.html,但我找不到在扩展 qml 中使用实际模型的方法。

目前我有这个模型:

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

我正在尝试从这个视图类中访问它:

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

但我找不到实际使用该模型的方法。我什至无法正确设置对模型的读取和写入,因为 QAbstractItemModels(以及一般 Qt 中的模型)已删除其复制构造函数以强制执行实体奇异性。这是我当前损坏的实现:

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

那么,我该如何做才能将这个扩展的 qml 类与我的 LayoutModel 一起使用?

【问题讨论】:

    标签: c++ qt qml qt5 qtquick2


    【解决方案1】:

    QObjects 没有复制构造函数,所以必须使用指针:

    *.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();
    }
    

    【讨论】:

    • 并且指针会通过QML工作,支持Layout { model: myModel }?
    猜你喜欢
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2013-03-25
    • 2011-10-07
    • 2014-07-16
    • 2019-11-18
    • 1970-01-01
    • 2011-11-24
    相关资源
    最近更新 更多