Properties in data for subclass of QAbstractListModel

  • Hi there,
    I want to have a custom ListModel, so I extended QAbstractListModel. My Data has a property and so it has to be a subclass of QObject.
    This is the code of the four files:

    #ifndef SIMPLEDATA_H
    #define SIMPLEDATA_H
    #include <QObject>
    class SimpleData : public QObject {
        Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged)
        SimpleData(QObject *parent = 0);
        void setValue(const QString &value);
        QString value() const;
        void valueChanged(QString);
        QString m_value;
    #endif // SIMPLEDATA_H
    #include "simpledata.h"
    SimpleData::SimpleData(QObject *parent) : QObject(parent) { }
    SimpleData::~SimpleData() { }
    void SimpleData::setValue(const QString &value) {
        m_value = value;
        emit valueChanged(m_value);
    QString SimpleData::value() const {
        return m_value;
    #ifndef SIMPLEMODEL_H
    #define SIMPLEMODEL_H
    #include <QAbstractListModel>
    #include "simpledata.h"
    class SimpleModel : public QAbstractListModel {
        enum SimpleDataRoles {
            ValueRole = Qt::UserRole + 1
        SimpleModel(QObject *parent = 0);
        void addData(const SimpleData &data);
        int rowCount(const QModelIndex &parent = QModelIndex()) const;
        QVariant data(const QModelIndex &index, int role) const;
        QHash<int, QByteArray> roleNames() const;
        QList<SimpleData> m_data;
    #endif // SIMPLEMODEL_H
    #include "simplemodel.h"
    SimpleModel::SimpleModel(QObject *parent) : QAbstractListModel(parent) { }
    void SimpleModel::addData(const SimpleData &data) {
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        m_data << data;
    int SimpleModel::rowCount(const QModelIndex &parent) const {
        return m_data.count();
    QVariant SimpleModel::data(const QModelIndex &index, int role) const {
        if(index.row() < 0 || index.row() >= m_data.count()) return QVariant();
        const SimpleData &data = m_data[index.row()];
        if(role == ValueRole) return data.value();
        return QVariant();
    QHash<int, QByteArray> SimpleModel::roleNames() const {
        QHash<int, QByteArray> roles;
        roles[ValueRole] = "value";
        return roles;

    I'm sorry I can't give you less code, but I don't have any clue about the source of the error. The compiler says:

    /home/xxx/Qt/5.7/gcc_64/include/QtCore/qlist.h:431: error: use of deleted function 'SimpleData::SimpleData(const SimpleData&)'
         if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);

    Additionally, there are several errors about private constructors of QObject.
    Can you explain me where I made the mistake and why those errors occur?

  • Qt Champions 2016

    @dxm_surfer said:

    QList<SimpleData> m_data;

    Hi and welcome
    Due to signals and design, QObject based classes are not allowed to be copied .

    QList<SimpleData> m_data;
    should be
    QList<SimpleData *> m_data;
    please see
    No Copy Constructor or Assignment Operator

  • QObjects can't be copied.
    if you can remove the NOTIFY from the property and hence the signal you can remove the QObject inheritance and change Q_OBJECT to Q_GADGET otherwise you'll have to save a pointer to the QObject not the Qobject itself

  • Thanks, I think I got it. I don't feel that comfortable with C++ though...

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.