为什么我从继承自QAbstractListModel的类中获得“使用删除函数”?

时间:2017-04-28 13:28:11

标签: c++ qt c++11 inheritance

我正在尝试实现ListView。到目前为止,我已经实现了一个名为PacientModel的类,它继承了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

我也有ListView的实现,但是当我编译代码时,我收到了这个错误。

  

C:\ Qt \ Qt5.8.0 \ 5.8 \ android_armv7 \ include \ QtCore \ qmetatype.h:765:错误:使用已删除的函数'PacientModel :: PacientModel(const   PacientModel&安培)”                return new(where)T(* static_cast(t));

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

每个this Q&A QAbstractListModel的复制构造函数被标记为私有。这意味着它不可复制,因此,当您继承它时,您的派生类在默认情况下也不可复制。

如果你想制作你的类的副本,那么你需要为该类手动定义一个复制构造函数,而不是依赖于编译器为你做的(因为它不会)。

答案 1 :(得分:1)

几天之后我发现错误不在类声明中,这是我将数据分配给listview的方式......我正在使用它:

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

而不是:

ctxt->setContextProperty("modelList", &pacientes);
相关问题