Here is my current workaround for reparenting a QQuickItem using PyQt5:
class Utilities(Singleton):
@pyqtSlot(QtQuick.QQuickItem, QtQuick.QQuickItem)
def reparent(self, item, parent):
# Sets the visual parent
item.setParentItem(parent)
# Sets the QObject parent
item.setParent(parent)
QtQml.qmlRegisterSingletonType(Utilities, "Utilities", 1, 0, "Utilities", Utilities.Instance)
Here's the usage in QML:
Utilities.reparent(target, newParent);
oldParent.destroy();
It should be trivial to translate into C++. How you chose to manage your singletons is an implementation detail, here is my quick and dirty solution:
def lazy_setdefaultattr(obj, key, default):
try:
result = getattr(obj, key)
except AttributeError:
result = default()
setattr(obj, key, result)
finally:
return result
class Singleton(QtCore.QObject):
@classmethod
def Instance(cls, *args, **kwargs):
return lazy_setdefaultattr(cls, '_instance', cls)