<?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[Using a List&lt;object&gt; to feed a QAbstractListModel]]></title><description><![CDATA[<p dir="auto">Hello!</p>
<p dir="auto">I'm building a desktop application and want to persist some data using classes. It's just for temporary data.<br />
I want to feed a model to delegate data inside a ListView with the class objects<br />
The issue is I've not been able to update the model and I don't know why. Could anyone help me?</p>
<ol>
<li>If I load data into the class constructor, it works. :(</li>
<li>If I try to attribute a list and emit change or use beginInsertRows, it doesn't work. :(</li>
</ol>
<hr />
<p dir="auto"><strong>EDIT:</strong></p>
<p dir="auto">After 2 days of thinking about it, I decided to change lProduct as a static variable and it worked. But I know that I misunderstanding about the class scope (how I instantiate the model correctly) and it is a signal that I must back to study C++ language.</p>
<hr />
<p dir="auto">main.cpp</p>
<pre><code>    QAbstractListModel *modelDataSession = new DataObjectModel(nullptr);
    engine.rootContext()-&gt;setContextProperty("productDataModel", modelDataSession);
</code></pre>
<p dir="auto">dataobjectmodel.h</p>
<pre><code>#ifndef DATAOBJECTMODEL_H
#define DATAOBJECTMODEL_H

#include &lt;QObject&gt;
#include &lt;QAbstractListModel&gt;

class DataObjectModel : public QAbstractListModel
{
    Q_OBJECT
    Q_ENUMS(MyRoles)
public:

    explicit DataObjectModel(QObject *parent = nullptr);

    enum MyRoles {
        ProductId = Qt::UserRole + 1,
        ProductName,
        ProductPhoto,
        ProductPrice,
        ProductQuantity
    };

    using QAbstractListModel::QAbstractListModel;

    QHash&lt;int, QByteArray&gt; roleNames() const override {

         qDebug() &lt;&lt; "roleNames";

        return {
            { ProductId, "id" },
            { ProductName, "name"    },
            { ProductPhoto, "photo"  },
            { ProductPrice, "price"  },
            { ProductQuantity, "quantity" },
        };
    }

    int rowCount(const QModelIndex &amp; parent = QModelIndex()) const override {
        qDebug() &lt;&lt; "rowCount Updating datamodel inside data";

        if (parent.isValid()) {
            return 0;
        }
        return lProduct.size();
    }

    bool setData(const QModelIndex &amp;index, const QVariant &amp;value, int role) override;
    QVariant data(const QModelIndex &amp; index, int role = Qt::DisplayRole) const override;

    QList&lt;product&gt; getLProduct();
    void setLProduct(QList&lt;product&gt; &amp;listProducts);

private:
    QList&lt;product*&gt; lProduct;

signals:

public slots:

    void updateModel();
};

#endif // DATAOBJECTMODEL_H

</code></pre>
<p dir="auto">dataobjectmodel.c</p>
<pre><code>#include "dataobjectmodel.h"

DataObjectModel::DataObjectModel(QObject *parent) : QAbstractListModel(parent)
{
    // this works 
    lProduct &lt;&lt; new product(1,"nome", "photo.png", 1.45, 10);
    lProduct &lt;&lt; new product(2,"nome 2", "photo.png", 1.9, 10);
}

bool DataObjectModel::setData(const QModelIndex &amp;index, const QVariant &amp;value, int role)
{
    if (!hasIndex(index.row(), index.column(), index.parent()) || !value.isValid())
        return false;

    product item = *lProduct[index.row()];

    switch (role) {
    case ProductId:
        item.setId(value.toInt());
        break;
    case ProductName:
        item.setName(value.toString());
        break;
    case ProductPhoto:
        item.setPhoto(value.toString());
        break;
    case ProductPrice:
        item.setPrice(value.toDouble());
        break;
    case ProductQuantity:
        item.setQuantity(value.toInt());
        break;
    default:
        return false;
    }

    emit dataChanged(index, index, { role } );

    return true;
}

QVariant DataObjectModel::data(const QModelIndex &amp;index, int role) const
{
    if (!hasIndex(index.row(), index.column(), index.parent()))
        return {};

    const product product = *lProduct[index.row()];

    switch (role) {
    case ProductId:
        return product.getId();
    case ProductName:
        return product.getName();
    case ProductPhoto:
        return product.getPhoto();
    case ProductPrice:
        return product.getPrice();
    case ProductQuantity:
        return product.getQuantity();
    default:
        return {};
    }

}


void DataObjectModel::setLProduct(QList&lt;product&gt; &amp;listProducts)
{
    QList&lt;product&gt;::iterator i;
    lProduct.clear();
    beginInsertRows(QModelIndex(), 0, listProducts.length()-1);
    for( i= listProducts.begin(); i != listProducts.end(); i++) {
        qDebug() &lt;&lt; "Item " &lt;&lt; (*i).getName();
        lProduct &lt;&lt; &amp;(*i);
    }
     endInsertRows();

     emit dataChanged(QModelIndex(),QModelIndex());
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/93664/using-a-list-object-to-feed-a-qabstractlistmodel</link><generator>RSS for Node</generator><lastBuildDate>Tue, 06 Oct 2026 15:35:57 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/93664.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 13 Aug 2018 21:32:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Using a List&lt;object&gt; to feed a QAbstractListModel on Mon, 13 Aug 2018 21:51:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> said in <a href="/post/475906">Using a List&lt;object&gt; to feed a QAbstractListModel</a>:</p>
<blockquote>
<p dir="auto">Hi and welcome to devnet,</p>
</blockquote>
<p dir="auto">Thank you, <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> !</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> said in <a href="/post/475906">Using a List&lt;object&gt; to feed a QAbstractListModel</a>:</p>
<blockquote>
<p dir="auto">Looks like you are working on a copy of your object and not the original. Out of curiosity, why are you dereferencing it ?</p>
</blockquote>
<p dir="auto">I tried everything to understand how this mechanism works. I've removed dereferencing in the variable later.</p>
<p dir="auto">The issue seems to be here</p>
<pre><code>void DataObjectModel::setLProduct(QList&lt;product&gt; &amp;listProducts)
{
  // I tried before
  // lProduct = listProduct;

    QList&lt;product&gt;::iterator i;
    lProduct.clear();
    beginInsertRows(QModelIndex(), 0, listProducts.length()-1);
    for( i= listProducts.begin(); i != listProducts.end(); i++) {
        qDebug() &lt;&lt; "Item " &lt;&lt; (*i).getName();
        lProduct &lt;&lt; (*i);
    }
     endInsertRows();

     emit dataChanged(QModelIndex(),QModelIndex());
}
</code></pre>
]]></description><link>https://forum.qt.io/post/475908</link><guid isPermaLink="true">https://forum.qt.io/post/475908</guid><dc:creator><![CDATA[carpajr]]></dc:creator><pubDate>Mon, 13 Aug 2018 21:51:46 GMT</pubDate></item><item><title><![CDATA[Reply to Using a List&lt;object&gt; to feed a QAbstractListModel on Mon, 13 Aug 2018 21:37:44 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/carpajr">@<bdi>carpajr</bdi></a> said in <a href="/post/475903">Using a List&lt;object&gt; to feed a QAbstractListModel</a>:</p>
<blockquote>
<p dir="auto">product item = *lProduct[index.row()];</p>
</blockquote>
<p dir="auto">Looks like you are working on a copy of your object and not the original. Out of curiosity, why are you dereferencing it ?</p>
]]></description><link>https://forum.qt.io/post/475906</link><guid isPermaLink="true">https://forum.qt.io/post/475906</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 13 Aug 2018 21:37:44 GMT</pubDate></item></channel></rss>