简体   繁体   English

如何创建QQuickItem的单独副本并将其呈现在其他窗口上

[英]How create a separate copy of a QQuickItem & render it on a different window

I have a QQuickItem fetched from C++ side like this. 我有一个像这样从C ++端获取的QQuickItem

QQuickItem * my_item = qmlEngine->rootObjects()[0]->findChild<QQuickItem*>("ItemObjectName");

my_item is valid & has all the properties set on to it. my_item有效且已设置所有属性。

Scenario 情境
I have 2 windows which need this QQuickItem to be drawn on alterantively. 我有2个需要QQuickItem绘制此QQuickItem 窗口 I want to render this my_item to a different window. 我想将此my_item渲染到另一个窗口。 This works perfectly fine as long as I set the Parent of the my_item to the other window 只要我将my_item的Parent设置到另一个窗口,这my_item地工作

// the_other_window is a QQuickWindow
QQuickWindow * the_other_window;

// here I set parent
my_item->setParentItem(the_other_window->contentItem());

This requires me to do setParentItem again back to my_item 's original window otherwise it goes invisible on the original window. 这要求我再次将setParentItem再次返回my_item的原始窗口,否则它将在原始窗口上不可见。 This is working but gives me unnecessary dependency. 这是可行的,但给了我不必要的依赖。 Instead I am trying to create a copy of the QQuickItem & do a setParentItem on that. 相反,我试图创建QQuickItem的副本并在QQuickItem执行setParentItem By copying like this: 通过这样复制:

QQuickItem * item_copy = new QQuickItem(my_item);

Problem: 问题:
But this doesn't seem to create a copy of the QQuickItem & hence I don't see a copy of my_item on the_other_window . 但这似乎并未创建QQuickItem的副本, my_itemthe_other_window的副本。

Question: 题:
All I want to know is, how can I create a valid copy a QQuickItem into another pointer say QQuickItem * item_copy & render it on a different window without affecting the visibility/state of the original QQuickItem ? 我想知道的是,如何创建一个QQuickItem的有效副本到另一个指针QQuickItem * item_copy并将其呈现在另一个窗口上,而又不影响原始QQuickItem的可见性/状态?

The interface of QQuickItem doesn't provide clonability. QQuickItem的界面不提供克隆性。 If it did, then all its subclasses would have to reimplement a virtual clone() function. 如果是这样,那么它的所有子类都必须重新实现一个虚拟clone()函数。

Indeed, the QQuickItem derives from QObject which explicitly disables copy-like operations (copy constructor and assignment operator), so they're disabled in any QQuickItem-derived class as well. 确实,QQuickItem派生自QObject,后者显式禁用了类似复制的操作(复制构造函数和赋值运算符),因此在任何QQuickItem派生的类中也将其禁用。 Even if you have a specific subclass of QQuickItem, which you think you know how to copy, you can't implement "real" copying for it. 即使您拥有QQuickItem的特定子类(您认为自己知道如何复制),也无法为其实现“真实”复制。

The closest thing to do in the latter case is to instantiate a new, blank item of your type and manually copy all values of relevant properties from the old to the new instance. 在后一种情况下,最接近的操作是实例化您类型的新空白项目,并将相关属性的所有值从旧实例手动复制到新实例。 You can encapsulate code that in a copy function. 您可以将代码封装在copy函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM