gpt4 book ai didi

c++ - Qt 中 QObjects 的引用赋值是如何完成的?

转载 作者:行者123 更新时间:2023-11-30 02:27:48 24 4
gpt4 key购买 nike

在 Qt 中,当尝试分配引用时,我收到使用已删除函数错误:

/home/niko/QT_snippets/QML1/users.cpp:16: error: use of deleted function 'User::User(const User&)'
User user=users_map.value("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5a525e56537f5b50525e5651115c5052" rel="noreferrer noopener nofollow">[email protected]</a>");
^
^
/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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="debbb3bfb7b29ebab1b3bfb7b0f0bdb1b3" rel="noreferrer noopener nofollow">[email protected]</a>");

或者可能是以下内容?

User user=&users_map.value("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b5e565a52577b5f54565a525515585456" rel="noreferrer noopener nofollow">[email protected]</a>");

因为...上面的代码无法编译。我需要在 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:
};

最佳答案

as I see in C++ everybody uses references.

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


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

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

在以下行中:

User user=&users_map.value("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a6f676b63664a6e65676b636424696567" rel="noreferrer noopener nofollow">[email protected]</a>");

&取地址users_map.value("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="16737b777f7a5672797b777f783875797b" rel="noreferrer noopener nofollow">[email protected]</a>") ,所以你基本上创建了一个类型为 User* 的(悬空)指针。到 QMap::value 复制返回的元素.

您可以像这样更改代码以获得引用:

User& user=users_map["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="73161e121a1f33171c1e121a1d5d101c1e" rel="noreferrer noopener nofollow">[email protected]</a>"];

请注意,没有 QMap::value 实现返回引用,因此您必须在此处使用 QMap::operator[] (您可能需要检查 "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a2f272b23260a2e25272b232464292527" rel="noreferrer noopener nofollow">[email protected]</a>" 是否确实是 map 中包含的键;否则它将被静默添加)。

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

QMap<QString, User> users_map;

从 Qt 角度来看,这看起来是一个糟糕的设计,您可能会遇到更多此类错误。


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

关于c++ - Qt 中 QObjects 的引用赋值是如何完成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41323288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com