QObject克隆

发布于 2024-09-02 06:19:57 字数 415 浏览 9 评论 0原文

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

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

or

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

并且克隆方法复制成员等内容,这会是错误的吗?

如果可以的话,我可以编写自己的复制构造函数和赋值运算符来执行此操作吗?

注意:我实际上想用继承 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);

or

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?

Note: I actually want to try this with classes that inherit qobject.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮筵 2024-09-09 06:19:57

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

如果有的话,克隆只对 QObject 的特定子类有意义,对 QObject 本身没有意义(它们没有真正的“类似值”属性)。

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

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.

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).

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.

坏尐絯 2024-09-09 06:19:57

我认为这种情况下的最佳实践是使用要在 QObject 之间复制的数据创建类。该类不应从 QObject 或任何从 QObject 派生的类派生。这个类将是“值容器”。在这种情况下,您应该能够以非常好的方式解决您的问题。

还有一点提示:对于此类,您可以使用隐式数据共享和写入时复制来减少不必要的复制开销:http://doc.qt.io/qt-5/implicit-sharing.html

I think that the best practice in this case to create class with the data you want to copy between QObjects. This class should not be derived from QObject or any class derived from QObject. And this class will be "value container". In this case you should be able to solve your problem in really good way.

One more tip: for this class you can use implicit data sharing with copy on write to reduce overhead of unnecessary copying: http://doc.qt.io/qt-5/implicit-sharing.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文