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

你离开我真会死。 提交于 2019-12-12 04:48:11

问题


I have a QQuickItem fetched from C++ side like this.

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

my_item is valid & has all the properties set on to it.

Scenario
I have 2 windows which need this QQuickItem to be drawn on alterantively. I want to render this my_item to a different window. This works perfectly fine as long as I set the Parent of the my_item to the other window

// 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. This is working but gives me unnecessary dependency. Instead I am trying to create a copy of the QQuickItem & do a setParentItem on that. 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.

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?


回答1:


The interface of QQuickItem doesn't provide clonability. If it did, then all its subclasses would have to reimplement a virtual clone() function.

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

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.



来源:https://stackoverflow.com/questions/46117093/how-create-a-separate-copy-of-a-qquickitem-render-it-on-a-different-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!