'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及以上 演示地址 演 ...
随机推荐
- leetCode 83.Remove Duplicates from Sorted List(删除排序链表的反复) 解题思路和方法
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 一个简单的HTML5摇一摇实例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- MagicalRecord使用教程【转载】
原文地址:http://www.ithao123.cn/content-96403.html 下面是在xcode5.1下ARC环境中的使用教程 1. 将 MagicalRecord 文件夹拖入到工程文 ...
- 一张图帮你看懂 iPhone 6 Plus 的屏幕分辨率
一张图帮你看懂 iPhone 6 Plus 的屏幕分辨率 几天前公布的 iPhone 6 Plus 官方标称屏幕是 1920 x 1080 的,可是在 Xcode 中我们发现模拟器的屏幕事实上是看似奇 ...
- python学习(十二)模块
怎么一下子就来学了模块? 其实学了判断.循环.函数等知识就可以开始下水写程序了,不用在意其他的细节,等你用到的时候再回过头去看,此所谓囫囵吞枣学习法. 为啥学模块? 有点用的.或者有点规模的程序都是要 ...
- Intellij IDEA如何不显示参数提示
刚安装了IDEA之后,调用方法的时候会提示方法中的参数,就像下面这样: 虽然IDEA也是好心,提示,但是劳资看着难受啊. 如果觉得不习惯,不想看参数名,可以用下图的方式取消.具体是: setting ...
- C语言序列点问题总结(大多数高等教育C语言教学课程的漏洞)
C语言序列点总结 2013年11月21于浙大华家池 C 语言副作用: (side effect)是指对数据对象或者文件的修改. 例如,语句 v = 99;的副作用是把 v 的值修改成 99. C语言序 ...
- Mysql 免密码登录,修改密码及忘记密码操作
----免密码登陆 方式一 my.cnf增加[client]标签 [client] user="root" password="你的密码" 单对定义不同的客户端 ...
- Rider
听说你开发.NET还在用VS,小哥哥给你推荐全平台的Rider 本文地址:http://www.cnblogs.com/likeli/p/8461010.html 前言 .NET平台的开发一直都只 ...
- 【PyCharm编辑器】之报:Spellchecker inspection helps locate typos and misspelling in your code, comments and literals, and fix them in one click.问题
如上图,输入一个单词时会出现波浪线,报:Spellchecker inspection helps locate typos and misspelling in your code, comment ...