Unsolved 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) dataSourceObject->setId(value.toInt()); else if(role == nameRole) dataSourceObject->setName(value.toString()); else if(role == unitRole) dataSourceObject->setUnit(value.toString()); else if(role == valueRole) dataSourceObject->setValue(value.toDouble()); 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:
QObject(0x1bbb43e0)
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?
regards
-
How does DataSourceModel::setData() look like? Seems you're doing something wrong in there.
-
I changed the post, the function is now there
-
Hi,
Do you mean you have the same object in the whole model ?
How big is that model ? -
@SGaist
Hello,there are about 200 items, all of them are of the same type: "DataSourceObject"
#ifndef DATASOURCEOBJECT_H #define DATASOURCEOBJECT_H #include <QString> #include <QVariantMap> class DataSourceObject : public QObject { Q_OBJECT Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged) public: 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; signals: void valueChanged(); private: int m_id; QString m_name; QString m_unit; double m_value; }; #endif // DATASOURCEOBJECT_H
-
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.