'QObject& QObject::operator=(const QObject&)' is private——无法将自定义的QObject子类放入Qt容器(container)中
先贴出问题的代码:
#include<QCoreApplication>
classMyObject:publicQObject
{
public:
MyObject(QObject*parent =):
QObject(parent)
{
}
private:
int m_id;
};
int main(int argc,char*argv[])
{
QCoreApplication a(argc, argv);
QList<MyObject>list;
list<<MyObject();
return a.exec();
}
QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value on the Qt Object Model page.
The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can't use a subclass of QObject as the value to be stored in one of the container classes. You must store pointers.
简单说就是:
QObject的拷贝构造函数是私有的,当把其子类放入容器时无法完成构造其副本。
解决方法就是,使用指针。将自定义的子类以指针的形式保存在容器中。
QList<MyObject *>list;
'QObject& QObject::operator=(const QObject&)' is private——无法将自定义的QObject子类放入Qt容器(container)中的更多相关文章
- C/C++对bool operator < (const p &a)const的认识,运算符重载详解(杂谈)
下面来进行这段代码的分析: struct node { //定义一个结构体node(节点) int x; int y; int len; //node中有3个成员变量x,y,l ...
- 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'
error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Tra ...
- PHP中const,static,public,private,protected的区别
原文地址:http://small.aiweimeng.top/index.php/archives/54.html const: 定义常量,一般定义后不可改变static: 静态,类名可以访问pub ...
- Error 2 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'
Error 2 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree ...
- error C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject'
1.error C2471: cannot update program database vc90.pdb 解决方案:https://blog.csdn.net/shuixin536/article ...
- bool operator==(const Array&)const; 这最后一个const 是做什么用的
字符重载也是个函数,在函数末尾加CONST 这样的函数叫常成员函数.常成员函数可以理解为是一个“只读”函数,它既不能更改数据成员的值,也不能调用那些能引起数据成员值变化的成员函数,只能调用const成 ...
- 结构体内嵌比较函数bool operator < (const node &x) const {}
直接看别人的链接 [http://www.cnblogs.com/ZERO-/p/9347296.html]
- C/C++中的const
1 C中的const C中const修饰的变量是只读变量,在使用const关键字声明定义变量时会给该变量分配内存空间. const修饰的全局变量默认是外部链接的,即其它源文件可以直接使用该变量. co ...
- Flex4/Flash开发在线音乐播放器 , 含演示地址
要求 必备知识 本文要求基本了解 Adobe Flex编程知识和JAVA基础知识. 开发环境 MyEclipse10/Flash Builder4.6/Flash Player11及以上 演示地址 演 ...
随机推荐
- 在Linux里环境变量设置的方法(export PATH)
一般来说,配置交叉编译工具链的时候须要指定编译工具的路径,此时就须要环境变量设置.比如我的mips-linux-gcc编译器在"/opt/au1200_rm/build_tools/bin& ...
- getOutString 输出弹出字符串
输入字符串长度,字符串,计数m.从前往后计数,当数到m个元素时,第m个元素出列,同时将该元素赋值给m,然后从下一个数计数循环,直到所有数字都出列,给定的数全部为大于0的数字.输出出队队列. 例如: ...
- 数据库sql的join多表
摘录文章 SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据.注意,join后的数据记录数不一定就是左或右表的简单连接,图表只代表集合关系,在数量上并不准确,如这个条件后结果, ...
- Android 开发小工具之:Tools 属性 (转)
Android 开发小工具之:Tools 属性 http://blog.chengyunfeng.com/?p=755#ixzz4apLZhfmi 今天来介绍一些 Android 开发过程中比较有用但 ...
- C#中的let字句应用示例
一.应用场景 在查询表达式中,存储子表达式的结果有时很有用,这样可以在随后的子句中使用. 可以使用 let 关键字完成这一工作,该关键字可以创建一个新的范围变量,并且用您提供的表达式的结果初始化该变量 ...
- OpenStack 使用Ceph 配置指导
概述 Ceph 作为分布式文件系统,不但具有高可靠性.高扩展性.高性能. 也是统一存储系统.支持对象存储.块存储.文件存储,本文介绍怎样使用Ceph 块存储作为OpenStack的Glance.Nov ...
- System.DateTime.Now.ToString()的一些用法
日期处理函数 //2007年4月24日 this.TextBox6.Text = System.DateTime.Now.ToString("D"); //200 ...
- S2S4H整合注意问题
整合过程中出现问题记录: 1.The import javax.servlet.http.HttpServletRequest cannot be resolved 解决办法:在tomcat的lib目 ...
- mooc课程mit6.00.1x--problem set1解决方法
counting vowels: 计算字符串中含有元音字母aeiou的数量 char = 'azcbobobegghakl' num = 0 #利用in方法直接查找字符串char中含有的元音字母数量 ...
- php操作apache服务器上的ftp
在此之前,请先在window7上搭建apache-ftp服务器,请查看文章:Windows 上搭建Apache FtpServer test.php <?php set_time_limit(0 ...