QObject克隆 [英] QObject cloning

查看:106
本文介绍了QObject克隆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Qobject应该是身份而不是值,例如,您不能复制它们,并且默认情况下,复制构造函数和赋值是禁用的,如qt文档中所述。但是是否可以使用克隆方法从现有QObject创建新的QObject?这是逻辑错误吗?
如果我说

I know that Qobjects are supposed to be identities not values eg you cannot copy them and by default the copy constructor and assignment are disabled as explained in qt documentation. But is it possible to create a new QObject from an existing one using a clone method? Would this be a logic error ? If I say

QObject b; 
QObject a; 
b.cloneFrom(a);

QObject a = new QOBject();
QObject b = new QOBject();
b->cloneFrom(a);

克隆方法复制诸如成员之类的东西,这是错误的吗?

and the clone method copies stuff like members etc would this be wrong?

如果可以的话,我可以编写自己的副本构造函数和赋值运算符来做到这一点吗?

And if this is ok can I write my own copy constructor and assignment operator that does just that?

注意:我实际上想尝试

推荐答案

在我看来,克隆QObject几乎总是在语义上被破坏,并导致不良的副作用,正如他们已经说过的,因为他们具有身份。因此,克隆打破了人们对QObjects的所有假设,例如其信号/插槽连接和动态特性。您应该考虑是否要克隆的对象确实是QObjects,或者是否可以将要克隆的值部分排除在外。

in my opinion cloning QObjects is almost always semantically broken and leads to unwanted side-effects, due to them having an "identity" as you already said. Therefore, cloning breaks all the assumptions one has about QObjects, like their signal/slot connections and dynamic properties. You should consider if the objects to clone really need to be QObjects, or if the "value-part" you want to have cloned could be factored out.

总而言之,克隆仅对您特定的QObjects子类有意义,而对QObjects本身(不具有真正的类似于值的属性)有意义。

And if at all, cloning makes only sense for your specific subclasses of QObjects, not for QObjects themselves (which have no real "value-like" properties).

B; A.cloneFrom(B)看起来很破损,因为如果B是B的子类的实例而不是B本身的实例,则它不起作用。
克隆应该通过虚拟B * B :: clone()const完成。

also, A; B; A.cloneFrom( B ) looks broken, as it doesn't work if B is instance of a subclass of B instead of B itself. Clone should be done via a virtual B* B::clone() const I'd say.

这篇关于QObject克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆