As we all know, Q_OBJECTs are instances and are not copyable.
Is there any kind of syntactic sugar to copy all static and dynamic properties of an arbitrary QObject derived class?
It seems such a nobrainer, but I can’t find any reference to such – obviously implementing one myself should be quite trivial – loop over metaObject(), loop over dynamicPropertyNames(), set accordingly.
You could implement a copy helper class as follows.
Then you can use this in any QObject subclass with very low work needed to be done:
However, this still needs
clone()to be called (the ugly “Java-style”). So we can additionally define a copy constructor just callingclone()and you also might think of aassign()method called within the assignment operator.Please note that this really copies the properties only! There are a lot of other things being tracked in QObject, like the current connections. They explicitly forbid copying QObjects because it would be very difficult to define rules on how this should be done, and these rules would be the correct ones for some use cases only, while in others you want other rules…