'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及以上 演示地址 演 ...
随机推荐
- 转:PCIe基础知识
PCIe基础知识 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zqixiao_09/article/details/51842542 PCIe ...
- C语言 指向结构体数组的指针
当结构体指针变量指向一个结构体变量数组的时候,此时指针变量的值就是结构体数组的首地址 关于如何定义结构体数组,和将结构体指针指向结构体变量数组,不是重点. 重点是,明白结构体指针的是怎么移动的, 我个 ...
- 常用global.css
html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldset, legend, img { margin: ...
- XSS前置课程--同源策略
什么是同源策略: 在用户浏览互联网中的网页的过程中,身份和权限的思想是贯穿始终的 同源策略(Same-Origin Policy),就是为了保证互联网之中,各类资源的安全性而诞生的产物,它实际上是一个 ...
- Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!
using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...
- Appium python自动化测试系列之使用HTMLTestRunner生成测试报告(十三)
13.1 测试报告概述 13.1.1 测试报告的定义 在前面章节我们已经讲了自动化基础的很多东西,如果说掌握了,而且自己动手去练习了,我相信在一些初级的面试中是没任何问题的,今天我们接触的应该算是一 ...
- VMware 中安装Centos
1,在百度软件中心下载VM12 http://rj.baidu.com/soft/detail/13808.html?ald 2,一路NEXT安装,安装完之后需要秘钥激活. VMware Workst ...
- 【转】android 签名验证防止重打包
网上资料很多,这里只做一个笔记反编译 dex 修改重新打包签名后 apk 的签名信息肯定会改变,所以可以在代码中判断签名信息是否被改变过,如果签名不一致就退出程序,以防止 apk 被重新打包. 1 j ...
- Process Stats:了解你的APP怎样使用内存
原文地址:http://android-developers.blogspot.com/2014/01/process-stats-understanding-how-your.html?m=1 原作 ...
- Kindeditor 修改内容时如何不让 及 <> 被自动转义
$html = str_replace(' ', ' ', $html); $html = str_replace('>', '>', $html); $html ...