Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Properties in data for subclass of QAbstractListModel

Properties in data for subclass of QAbstractListModel

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dxm_surfer
    wrote on last edited by
    #1

    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_OBJECT
        Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged)
    
    public:
        SimpleData(QObject *parent = 0);
        ~SimpleData();
    
        void setValue(const QString &value);
        QString value() const;
    
    signals:
        void valueChanged(QString);
    
    private:
        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 {
    
        Q_OBJECT
    
    public:
        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;
    
    
    protected:
        QHash<int, QByteArray> roleNames() const;
    
    private:
        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;
        endInsertRows();
    }
    
    int SimpleModel::rowCount(const QModelIndex &parent) const {
        Q_UNUSED(parent);
        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?
    Thanks

    sorry about the typo in the name...
    i meant 'dmx' and not 'dxm'...

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @dxm_surfer said:

      QList<SimpleData> m_data;

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

      So
      QList<SimpleData> m_data;
      should be
      QList<SimpleData *> m_data;
      please see
      http://doc.qt.io/qt-5/qobject.html
      section
      No Copy Constructor or Assignment Operator

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • D Offline
          D Offline
          dxm_surfer
          wrote on last edited by
          #4

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

          sorry about the typo in the name...
          i meant 'dmx' and not 'dxm'...

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved