0

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?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • Can't you use [`ShaderEffectSource`](http://doc.qt.io/qt-5/qml-qtquick-shadereffectsource.html) instead? – GrecKo Sep 08 '17 at 14:20

1 Answers1

2

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.

Stefan Monov
  • 11,332
  • 10
  • 63
  • 120
  • Creating a blank `QQuickItem` & copying all the properties to the new one. Is it possible to refer some example which does this? I am now trying figure how to copy the existing properties to a blank `QQuickItem` – TheWaterProgrammer Sep 08 '17 at 13:16
  • thanks for clarifying the copy part. I created a different question [here](https://stackoverflow.com/questions/46118043/how-to-create-a-new-qquickitem-on-c-side-with-the-same-properties-as-an-existi). pls suggest something if you wish – TheWaterProgrammer Sep 08 '17 at 13:51
  • You should **share** the data part rather than copy anything, and simply instantiate two different visual objects, referring to the same data object. – dtech Sep 13 '17 at 10:18
  • @dtech: That's more awkward in the source though, because you have one extra object (one more step of indirection). And it creates inconsistency between different components - what you propose is to have those that will need copying to keep a "data pack object", while all the others - to keep their data directly as properties. Also you wouldn't be able to move to your data pack object the properties of the Item type itself - such as `x`, `width`, etc. – Stefan Monov Sep 13 '17 at 11:56
  • @StefanMonov - on the contrary. Your approach will break the moment one object changes, that change will not be reflected by its copy, thus they are no longer copies. Whereas if you bind to a single data object, changes will be reflected by all views. And you can still transform copies if you so chose, by either using dedicated transformations or wrapping in a transforming parent. – dtech Sep 13 '17 at 12:07