1

I am trying to implement a ListView. So far, I have implemented a class named PacientModel that inherits of QAbstractListModel.

#ifndef PACIENTMODEL_H
#define PACIENTMODEL_H

#include <QAbstractListModel>

class PacientModel : public QAbstractListModel {
    Q_OBJECT

public:
    enum PacientRole {
        CIRole,
        NameRole,
        LastNameRole
    };
    Q_ENUM(PacientRole)

    PacientModel(QObject * parent = nullptr);

    int rowCount(const QModelIndex & = QModelIndex()) const;
    QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const;
    QHash<int, QByteArray> roleNames() const;

    Q_INVOKABLE QVariantMap get(int row) const;
    Q_INVOKABLE void append (const QString &CI, const QString &name, const QString &lastName);
    Q_INVOKABLE void set (int row, const QString & CI, const QString &name, const QString &lastName);
    Q_INVOKABLE void remove (int row);

private:
    struct Pacient {
        QString CI;
        QString name;
        QString lastName;
    };

    QList<Pacient> m_pacients; };

#endif // PACIENTMODEL_H

I also have the implementation of the ListView, but when I compiled the code, I got this error.

C:\Qt\Qt5.8.0\5.8\android_armv7\include\QtCore\qmetatype.h:765: error: use of deleted function 'PacientModel::PacientModel(const PacientModel&)' return new (where) T(*static_cast(t));

How can I resolve this?

demongolem
  • 9,474
  • 36
  • 90
  • 105
Liannet Yisel
  • 21
  • 1
  • 4

2 Answers2

3

Per this Q&A QAbstractListModel's copy constructor is marked as private. That means that it is not copyable and therefore, as you inherited from it, your derived class is also not copyable by default.

If you want to make copies of your class then you need to manually define a copy constructor for the class instead of relying on the compiler doing it for you (since it won't).

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • I use as reference the qt example **abstactitemmodel** and **contactlist** and none of them implements a copy constructor.. why?? – Liannet Yisel Apr 28 '17 at 15:01
  • @LiannetYisel I would have to see the examples. It could be because it is implied that these should not be copied. I've not used QT so I can just go with what I've found out. – NathanOliver Apr 28 '17 at 15:06
  • 1
    Usually Qt's models are dynamically allocated (pointers). Copy constructor is not needed. Be aware that Qt's views do not take ownership of the model, so you must take care of destroying them when no longer needed. – cbuchart Apr 28 '17 at 15:10
  • Implementing a 100% functional copy constructor for QObject subclasses is also impossible. So please don't. – rubenvb Apr 28 '17 at 15:25
1

After several days I discovered that the error wasn't in the class declaration, It was in the way I assigned the data to the listview... I was using this:

objetoLP->setProperty("modelList", QVariant::fromValue(pacientes));

instead of:

ctxt->setContextProperty("modelList", &pacientes);
Liannet Yisel
  • 21
  • 1
  • 4