0

I am trying to pass a c++ user model to qml and get an error that I don't understand.

I use a manager class that should reads in the users and fills the listmodel.

The list model itself should be pass to qml via Q_PROPERTY.

The manager class is known in the qml context.

In the public method of the manager class, the compiler tells me that private m_listModel is a deleted function.

UserManager.h

#pragma once
#include <QObject>
#include <QMap>
#include "UserListModel.h"

class UserManager : public QObject
{
    Q_OBJECT

public:
    explicit UserManager(QObject* parent = nullptr);
    Q_PROPERTY(UserListModel model READ getModel)
    UserListModel getModel() { return m_listModel; }
 
private:
    UserListModel m_listModel;
};

UserListModel getModel() { return m_listModel; } gives the error on m_listModel

Error is this

error C2280: "UserListModel::UserListModel(const UserListModel &)" : Es wurde versucht, auf eine gelöschte Funktion zu verweisen

UserManager.cpp

#include "UserManager.h"

UserManager::UserManager(QObject* parent) 
{
    
}

void UserManager::initialize(QMap<QString, QString> args)
{
    
    m_listModel.addModel(UserModel("user1", "0000"));
    m_listModel.addModel(UserModel("user2", "9999"));
}

UserListModel.h

#pragma once
#include <QObject>
#include <QAbstractListModel>

struct UserModel {
    UserModel() {}
    UserModel(const QString name, const QString password) : name(name), password(password) {}
    QString name;
    QString password;
};

class UserListModel : public QAbstractListModel
{
    Q_OBJECT

public:
    explicit UserListModel(QObject* parent = 0);
    enum Roles { NameRole = Qt::UserRole, PasswordRole };
    int rowCount(const QModelIndex& parent = QModelIndex()) const override;
    QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;
    void addModel(const UserModel model);

private:
    QList<UserModel> m_models;
};

UserListModel.cpp

#include "UserListModel.h"

UserListModel::UserListModel(QObject* parent) 
{
}

int UserListModel::rowCount(const QModelIndex& parent) const
{
    return m_models.count();
}

QVariant UserListModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid()) return QVariant();
    const UserModel user = m_models.at(index.row());
    if (role == NameRole) return user.name;
    else if (role == PasswordRole) return user.password;
    else return QVariant();
}

QHash<int, QByteArray> UserListModel::roleNames() const
{
    static QHash<int, QByteArray> mapping{ {NameRole, "name"}, {PasswordRole, "password"} };
    return mapping;
}

void UserListModel::addModel(const UserModel model)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_models << model;
    endInsertRows();
}

Sorry if it is a beginner's mistake, I'm just starting to learn qt and c++.

user2377283
  • 365
  • 1
  • 2
  • 12
  • 3
    Are you sure that `UserListModel getModel() { return m_listModel; }` is correct? I bet it has to be `UserListModel& getModel() { return m_listModel; }` (or even `const UserListModel& getModel() const { return m_listModel; }`) i.e. returning a reference instead of a value. Returning by value may trigger the call of the copy constructor which is probably deleted. That would explain your error message. – Scheff's Cat Jan 20 '23 at 17:14
  • 1
    Qt attempts to make it more difficult to copy any QObject. Instead you should not return a copy of the model by value or pass one around by value. – drescherjm Jan 20 '23 at 17:58
  • Thanks this solved the issue private: UserListModel2 m_listModel; Q_PROPERTY(UserListModel2* model READ getModel); UserListModel2* getModel() { return &m_listModel; } – user2377283 Jan 23 '23 at 06:28

0 Answers0