<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QAbstractListModel items  cannot inherit from QObject]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I have a QAbstractListModel that should have items that inherit from QObject(named gaugeobject)</p>
<p dir="auto">When the Item does not inherit from QObject it's working, but when inherited I get a error:</p>
<pre><code>use of deleted function 'GaugeObject::GaugeObject(const GaugeObject&amp;)'
     if (QTypeInfo&lt;T&gt;::isLarge || QTypeInfo&lt;T&gt;::isStatic) n-&gt;v = new T(t);
                                                               ^
</code></pre>
<p dir="auto">If I understand it right, there is something wrong witht the default constructor?<br />
This is the class:</p>
<pre><code>#ifndef GAUGEOBJECT_H
#define GAUGEOBJECT_H

#include &lt;QObject&gt;
#include &lt;QVariantMap&gt;

class GaugeObject : public QObject
{
public:

    GaugeObject(const int &amp;id, const QString &amp;mainValue, const QString &amp;secValue, const int &amp;width, const int &amp;height,
                const int &amp;x, const int &amp;y, const int &amp;size);
    GaugeObject(QObject *parent = nullptr);
.....


</code></pre>
<pre><code>#include "gaugeobject.h"

GaugeObject::GaugeObject(QObject *parent) : QObject (parent)
{
}

GaugeObject::GaugeObject(const int &amp;id, const QString &amp;mainValue, const QString &amp;secValue, const int &amp;width, const int &amp;height, const int &amp;x, const int &amp;y, const int &amp;size):
    m_id(id), m_mainValue(mainValue), m_secValue(secValue), m_width(width), m_height(height), m_x(x), m_y(y), m_size(size)
{
}
......
</code></pre>
]]></description><link>https://forum.qt.io/topic/98941/qabstractlistmodel-items-cannot-inherit-from-qobject</link><generator>RSS for Node</generator><lastBuildDate>Fri, 23 Oct 2026 19:13:45 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/98941.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 25 Jan 2019 12:27:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QAbstractListModel items  cannot inherit from QObject on Fri, 25 Jan 2019 14:27:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/slash200">@<bdi>Slash200</bdi></a></p>
<p dir="auto">Store the pointers to your GaugeObjects in your model as described from the link of <a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a></p>
<blockquote>
<p dir="auto">The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can't use a subclass of QObject as the value to be stored in one of the container classes. You must store pointers.</p>
</blockquote>
<pre><code>QList&lt;GaugeObject *&gt; m_GaugeObjects;
</code></pre>
]]></description><link>https://forum.qt.io/post/507175</link><guid isPermaLink="true">https://forum.qt.io/post/507175</guid><dc:creator><![CDATA[beecksche]]></dc:creator><pubDate>Fri, 25 Jan 2019 14:27:07 GMT</pubDate></item><item><title><![CDATA[Reply to QAbstractListModel items  cannot inherit from QObject on Fri, 25 Jan 2019 13:25:35 GMT]]></title><description><![CDATA[<p dir="auto">You can pass the parameters from QML to C++ side and construct object c++ itself.</p>
]]></description><link>https://forum.qt.io/post/507163</link><guid isPermaLink="true">https://forum.qt.io/post/507163</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Fri, 25 Jan 2019 13:25:35 GMT</pubDate></item><item><title><![CDATA[Reply to QAbstractListModel items  cannot inherit from QObject on Fri, 25 Jan 2019 13:14:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> said in <a href="/post/507153">QAbstractListModel items cannot inherit from QObject</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/slash200">@<bdi>Slash200</bdi></a> See <a href="http://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator</a></p>
</blockquote>
<p dir="auto">Ok so it's normal that I used a class not inherited from QObject for models.<br />
Thank you.</p>
<p dir="auto">But that brings me to a problem. I want to add objects to that model from QML, but I cant access  the class from QML when it's not inherited from QObject.</p>
]]></description><link>https://forum.qt.io/post/507162</link><guid isPermaLink="true">https://forum.qt.io/post/507162</guid><dc:creator><![CDATA[Slash200]]></dc:creator><pubDate>Fri, 25 Jan 2019 13:14:13 GMT</pubDate></item><item><title><![CDATA[Reply to QAbstractListModel items  cannot inherit from QObject on Fri, 25 Jan 2019 12:39:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/slash200">@<bdi>Slash200</bdi></a> See <a href="http://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator</a></p>
]]></description><link>https://forum.qt.io/post/507153</link><guid isPermaLink="true">https://forum.qt.io/post/507153</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 25 Jan 2019 12:39:26 GMT</pubDate></item><item><title><![CDATA[Reply to QAbstractListModel items  cannot inherit from QObject on Fri, 25 Jan 2019 12:35:14 GMT]]></title><description><![CDATA[<p dir="auto">model header:</p>
<pre><code>#ifndef GaugeModel_H
#define GaugeModel_H

#include "gaugeobject.h"
#include &lt;QAbstractListModel&gt;

class GaugeModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum gaugeRoles {
        idRole = Qt::UserRole,


    };

    explicit GaugeModel(QObject *parent = nullptr);
    Q_INVOKABLE void addGaugeObject(const GaugeObject &amp;GaugeObject);
    Q_INVOKABLE QVariantMap get(int row) const;

    int rowCount(const QModelIndex &amp;parent = QModelIndex()) const override;
    Q_INVOKABLE QVariant data(const QModelIndex &amp;index, int role = Qt::DisplayRole) const override;
    bool setData(const QModelIndex &amp;index, const QVariant &amp;value,
                 int role = Qt::EditRole) override;
    Qt::ItemFlags flags(const QModelIndex&amp; index) const override;
    QHash&lt;int, QByteArray&gt; roleNames() const override;

private:
    QList&lt;GaugeObject&gt; m_GaugeObjects;

};

#endif // GaugeModel_H
</code></pre>
<p dir="auto">Cpp:</p>
<pre><code>#include "GaugeModel.h"

GaugeModel::GaugeModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

QVariantMap GaugeModel::get(int row) const
{
    return m_GaugeObjects[row].toMap();
}

void GaugeModel::addGaugeObject(const GaugeObject &amp;GaugeObject)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_GaugeObjects &lt;&lt; GaugeObject;
    endInsertRows();
}


int GaugeModel::rowCount(const QModelIndex &amp;parent) const
{
    if(parent.isValid())
        return 0;
    return m_GaugeObjects.count();
}

QVariant GaugeModel::data(const QModelIndex &amp;index, int role) const
{
    if(index.row() &lt; 0 || index.row() &gt;= m_GaugeObjects.count() || !index.isValid())
        return  QVariant();
    const GaugeObject &amp;gaugeObject = m_GaugeObjects[index.row()];
    if ( role == idRole)
        return gaugeObject.id();
    return QVariant();
}

bool GaugeModel::setData(const QModelIndex &amp;index, const QVariant &amp;value, int role)
{
    GaugeObject &amp;gaugeObject = m_GaugeObjects[index.row()];
    if(data(index, role) != value) {
        if(role == idRole)
            gaugeObject.setId(value.toInt());
        emit dataChanged(index, index, QVector&lt;int&gt;() &lt;&lt; role);
        return true;
    }
    return false;
}

Qt::ItemFlags GaugeModel::flags(const QModelIndex &amp;index) const
{
    if(!index.isValid())
        return Qt::NoItemFlags;

    return Qt::ItemIsEditable;
}



QHash&lt;int, QByteArray&gt; GaugeModel::roleNames() const
{
    QHash&lt;int, QByteArray&gt; roles;
    roles[idRole] = "id";
    return roles;
}

</code></pre>
]]></description><link>https://forum.qt.io/post/507152</link><guid isPermaLink="true">https://forum.qt.io/post/507152</guid><dc:creator><![CDATA[Slash200]]></dc:creator><pubDate>Fri, 25 Jan 2019 12:35:14 GMT</pubDate></item><item><title><![CDATA[Reply to QAbstractListModel items  cannot inherit from QObject on Fri, 25 Jan 2019 12:31:36 GMT]]></title><description><![CDATA[<p dir="auto">No such issue. It works.<br />
You cannot copy the object of QObject types. Can you show how are you creating the list inside the QAbstractListModel ?</p>
]]></description><link>https://forum.qt.io/post/507150</link><guid isPermaLink="true">https://forum.qt.io/post/507150</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Fri, 25 Jan 2019 12:31:36 GMT</pubDate></item></channel></rss>