如何通过QT中的参考为qobject进行分配
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:
};
正如我在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;)
相关文章:
- 将数组的地址分配给变量并删除
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- 函数中堆分配的效果与缺少堆分配的情况
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 获取字符串的长度并将其分配给数组
- 将地址分配给本地指针后,公共对象的变量将消失
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 我在二维向量中是否正确分配了内存
- 正在尝试重载二进制搜索树分配运算符
- GlobalAlloc而不是其他分配方法
- 自定义先决条件对移动分配运算符有效吗
- 我可以重新分配/覆盖std::字符串吗
- 如何通过QT中的参考为qobject进行分配
- 分配两个 QObject
- 删除时,QObject是否区分堆栈和堆分配的子级
- 如何管理返回到 QML 的动态分配的 QObject 的生存期