如何通过QT中的参考为qobject进行分配

how is assignment by reference done in Qt for QObjects?

本文关键字:qobject 分配 参考 何通过 QT      更新时间:2023-10-16

在qt中,尝试分配参考时,我会得到use of deleted function错误:

/home/niko/QT_snippets/QML1/users.cpp:16: error: use of deleted function 'User::User(const User&)'
     User user=users_map.value("email@domain.com");
                                                 ^
         ^
/home/niko/QT_snippets/QML1/users.h:7: In file included from ../QML1/users.h:7:0,
/home/niko/QT_snippets/QML1/users.cpp:1: from ../QML1/users.cpp:1:
/home/niko/QT_snippets/QML1/user.h:6: 'User::User(const User&)' is implicitly deleted because the default definition would be ill-formed:
 class User : public QObject
       ^
/opt/Qt/5.7/gcc_64/include/QtCore/QObject:1: In file included from /opt/Qt/5.7/gcc_64/include/QtCore/QObject:1:0,
/home/niko/QT_snippets/QML1/users.h:4: from ../QML1/users.h:4,
/home/niko/QT_snippets/QML1/users.cpp:1: from ../QML1/users.cpp:1:

在C中,我一直在使用指针,我从来没有任何问题,但是正如我在C 中看到的那样,每个人都使用参考。

我应该如何通过qt中的参考分配对象?例如,在这一行中,我应该如何使user对象成为users_map对象中值的引用?

User user=users_map.value("email@domain.com");

或以下?

User user=&users_map.value("email@domain.com");

因为...上面的代码没有编译。我需要在Users类的方法中使用它来访问users_map变量中的数据。

Users类被声明为:

class Users : public QAbstractItemModel
{
    Q_OBJECT
    enum UserRoles {
        EmailRole = Qt::UserRole + 1,
        NameRole,
        PasswordRole
    };
private:
    QMap<QString,User>         users_map;
public:
    explicit Users(QAbstractItemModel *parent = 0);
    Q_INVOKABLE QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const;
    Q_INVOKABLE QModelIndex parent(const QModelIndex &child) const;
    Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const;
    Q_INVOKABLE int columnCount(const QModelIndex &parent = QModelIndex()) const;
    Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    QHash<int, QByteArray> roleNames() const;
signals:
public slots:
};

User类是这样声明的:

class User : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString email READ get_email WRITE set_email NOTIFY emailChanged);
    Q_PROPERTY(QString name READ get_name WRITE set_name NOTIFY nameChanged);
    Q_PROPERTY(QString password READ get_password WRITE set_password NOTIFY passwordChanged);
private:
    QString             email;
    QString             name;
    QString             password;
public:
    explicit User(QObject *parent = 0);
    QString get_email();
    void set_email(QString data);
    QString get_name();
    void set_name(QString data);
    QString get_password();
    void set_password(QString data);
signals:
    void emailChanged();
    void nameChanged();
    void passwordChanged();
public slots:
};

正如我在C 中看到的那样,每个人都使用参考。

您不应该相信您所看到的:)


QObject具有删除的复制构造函数,因此,事实上,您的派生类User也具有删除的复制构造函数,无法复制。这就是此错误的含义:

use of deleted function 'User::User(const User&)'

在以下行中:

User user=&users_map.value("email@domain.com");

&采用users_map.value("email@domain.com")的地址,因此您基本上是创建一个(悬空的)User*类型的指针,转换为QMap::value返回的元素。

您可以这样更改代码以获取参考:

User& user=users_map["email@domain.com"];

请注意,没有QMap::value实现返回参考,因此您必须在此处使用QMap::operator[](您可能需要检查"email@domain.com"是否确实是地图中包含的密钥;否则将默默添加否则)。

)。 )。

但是,请注意,QObject(和派生的类)被设计为用于指针,因此您的声明:

QMap<QString, User> users_map;

在QT透视上看起来像是一个不良的设计,您可能会遇到此类错误。


顺便说一句,正确的拼写是QT,而不是代表QuickTime的QT;)