Important: Please read the Qt Code of Conduct -

Item of QAbstractListModel changes type after beeing updated

  • Hello,

    I found a strange behaviour that makes me problems:

    The item of a QAbstractListModel, (mine's called datasourceobject) changes it's type after being updatet several times.

    In my case the item is updated via this function from the model.

    bool DataSourceModel::setData(const QModelIndex &index, const QVariant &value, int role)
        DataSourceObject *dataSourceObject = m_DataSourceObjects[index.row()];
        if (data(index, role) != value) {
            if(role == idRole)
            else if(role == nameRole)
            else if(role == unitRole)
            else if(role == valueRole)
            emit dataChanged(index, index, QVector<int>() << role);
            return true;
        return false;

    Returning a pointer from the item with this function returns : DataSourceObject(0x1bbb43e0)

    DataSourceObject *DataSourceModel::dataPointer(const QModelIndex &index)
        return m_DataSourceObjects[index.row()];

    After updating it several times it changes to:


    and the adress stays the same.

    I need to access single items from the model with QML via pointer to get it automatically updated, and this stops working when the type changes to QObject. Any explanation why this happens?


  • Qt Champions 2019

    How does DataSourceModel::setData() look like? Seems you're doing something wrong in there.

  • I changed the post, the function is now there

  • Lifetime Qt Champion


    Do you mean you have the same object in the whole model ?
    How big is that model ?

  • @SGaist

    there are about 200 items, all of them are of the same type: "DataSourceObject"

    #include <QString>
    #include <QVariantMap>
    class DataSourceObject : public QObject
        Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged)
        explicit DataSourceObject(QObject *parent = nullptr);
        DataSourceObject(const QJsonObject &obj);
        int id() const;
        void setId(int id);
        QString name() const;
        void setName(const QString &name);
        QString unit() const;
        void setUnit(const QString &unit);
        Q_INVOKABLE double value() const;
        void setValue(double value);
        QVariantMap toMap() const;
        void valueChanged();
        int m_id;
        QString m_name;
        QString m_unit;
        double m_value;

  • Lifetime Qt Champion

    Hence my question: do they all point to the same address ? Or are you visualising only one entry ?

  • @SGaist No they don't point to the same address.
    I want to visualize a single item.

Log in to reply