Note that there are some explanatory texts on larger screens.

plurals
  1. POQt - reduce duplication between two QAbstractListModel subclasses
    primarykey
    data
    text
    <p>I've written two QAbstractListModel subclasses:</p> <pre><code>class Model1: public QAbstractListModel { Q_OBJECT public: int rowCount(const QModelIndex&amp; parent=QModelIndex()) const; QVariant data(const QModelIndex&amp; index, int role) const; void clear(); private: QVector&lt;Obj1*&gt; m_items; }; </code></pre> <p>and </p> <pre><code>class Model2: public QAbstractListModel { Q_OBJECT public: int rowCount(const QModelIndex&amp; parent=QModelIndex()) const; QVariant data(const QModelIndex&amp; index, int role) const; void clear(); private: QVector&lt;Obj2*&gt; m_items; }; </code></pre> <p>In addition, these two classes have a slot AddObj() which is dependent on the pointer used (<code>Obj1*</code> or <code>Obj2*</code>).</p> <p>The matter is, constructors and destructors are the same, and so are <code>rowCount</code> and <code>clear</code> (they do the <em>exact</em> same thing). The only differences are in <code>data</code> and in what <code>m_items</code> is.</p> <p>What's the best strategy to reduce duplication here? I tried with a base class:</p> <pre><code>class ModelBase: public QAbstractListModel { Q_OBJECT ModelBase(QObject* parent=0); ~ModelBase(); public: virtual int rowCount(const QModelIndex&amp; parent=QModelIndex()) const; virtual QVariant data(const QModelIndex&amp; index, int role) const = 0; virtual void clear(); private: QVector&lt;QString*&gt; m_items; // dummy vector }; </code></pre> <p>and then have the other classes derive from it:</p> <pre><code>class ModelDerived1: public ModelBase { Q_OBJECT public: ModelDerived1(QObject* parent=0); ~ModelDerived1(); QVariant data(const QModelIndex&amp; index, int role) const = 0; private: QVector&lt;Obj1*&gt; m_items; }; </code></pre> <p>The problem is that in such a case the derived class's <code>data</code> function is never called when this model is attached to a view.</p> <p>Nevertheless, the derived class <code>data()</code> is not called (I put in debug statements, and they are never executed).</p> <p>What am I doing wrong here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload