QT的QObject禁用拷贝构造和赋值运算符原因

QT的QObject禁用拷贝构造和赋值运算符原因

QObject禁用复制构造函数和赋值运算符源于它的性质:

  1. 具有唯一的QObject :: objectName。
    Qt对象可以拥有自己的名称,objectName属性保存对象的名称。
    那么复制一个Qt对象,如何处理新的对象的名字?

  2. 在对象层次结构中具有位置。
    QObjects将自己组织在对象树中。
    那么复制一个Qt对象,新的对象应位于何处?

  3. 可以连接到其他Qt对象,以向它们发出信号或接收它们发出的信号。
    QObjects可以connect到其他对象。
    那么复制一个Qt对象,应该如何将这些连接转移到新的对象中?

  4. 可以在运行时添加未在C ++类中声明的新属性。
    那么复制一个Qt对象,原始对象中添加的属性是否应该复制到新对象?

由于这些原因,应将Qt对象视为身份而不是值。QT中的OBject类有信号和槽机制,并不像普通类那样简单并且有个对象具有唯一性。因此,QObject的拷贝构造函数和赋值操作符是禁用的。

``` class TestData : public QObject { Q_OBJECT public: TestData(QObject *parent = nullptr) : QObject(parent) {} //关键 必须这么写 TestData(const TestData &other) : QObject(other.parent()) { //this = other; // Copy other data members here this->Barcode = other.Barcode; this->CameraCounts = other.CameraCounts; this->GroupDefine = other.GroupDefine; this->Basic = other.Basic; this->Result = other.Result; this->Normal = other.Normal; this->Defocus = other.Defocus; this->m_bBarcode = other.m_bBarcode; this->m_bCameraCounts = other.m_bCameraCounts; this->m_bDefocus = other.m_bDefocus; this->m_bGroupDefine = other.m_bGroupDefine; } //Person &operator=(const Person &other) = delete; // 禁用赋值运算符 //关键 必须这么写 TestData &operator=(const TestData &other) { if (this != &other) { //this = other; // Copy other data members here this->Barcode = other.Barcode; this->CameraCounts = other.CameraCounts; this->GroupDefine = other.GroupDefine; this->Basic = other.Basic; this->Result = other.Result; this->Normal = other.Normal; this->Defocus = other.Defocus; this->m_bBarcode = other.m_bBarcode; this->m_bCameraCounts = other.m_bCameraCounts; this->m_bDefocus = other.m_bDefocus; this->m_bGroupDefine = other.m_bGroupDefine; } return *this; } public: QString Barcode; QString CameraCounts; QVector<QString> GroupDefine; BasicData Basic; ResultData Result; NormalData Normal; DefocusData Defocus; bool m_bBarcode; bool m_bCameraCounts; bool m_bGroupDefine; bool m_bDefocus; public: QVariantMap toVariantMap() const { QVariantMap map; if (m_bBarcode) { map["Barcode"] = QVariant::fromValue(Barcode); } if (m_bCameraCounts) { map["CameraCounts"] = QVariant::fromValue(CameraCounts); } if (m_bGroupDefine) { QVariantList list; for (auto item : GroupDefine) { list.append(item); } map["GroupDefine"] = list; } if (m_bDefocus) { QVariantMap tempDefocusMap = Defocus.toVariantMap(); if (tempDefocusMap.size() > 0) { map["Defocus"] = tempDefocusMap; } } QVariantMap tempBasicMap = Basic.toVariantMap(); if (tempBasicMap.size()>0) { map["Basic"] = tempBasicMap; } QVariantMap tempNormalMap = Normal.toVariantMap(); if (tempNormalMap.size() > 0) { map["Normal"] = tempNormalMap; } QVariantMap tempResultMap = Result.toVariantMap(); if (tempResultMap.size() > 0) { map["Result"] = tempResultMap; }```改用MFC
最新发布
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长不大的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值