如何通过引用在Qt中为QObjects进行分配?(how is assignment by reference done in Qt for QObjects?)

编程入门 行业动态 更新时间:2024-10-17 17:20:33
如何通过引用在Qt中为QObjects进行分配?(how is assignment by reference done in Qt for QObjects?)

在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: };

In Qt, when trying to assign a reference I am getting use of deleted function error:

/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:

In C, I was always using pointers and I never had any problem, but as I see in C++ everybody uses references.

How should I assign an object by reference in Qt? For example, in this line how should I make the user object be a reference to a value in users_map object?

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

Or maybe the following?

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

Because... the code above does not compile. I need to use it inside methods of Users class to access the data in users_map variable.

The Users class is declared as:

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: };

The User class is declared like this:

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: };

更多推荐

本文发布于:2023-04-29 02:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中为   分配   Qt   QObjects   reference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!