PDA

View Full Version : Deep copy of widget hierarchy



ajoffe
14th December 2009, 02:27
OK kind of long explanation here, bear with me please...

I have a QWidget subclass, let's call it ScreenClass. After instantiating a ScreenClass, I may set a layout for it, and then add any number of child widgets (and possibly sub-layouts) to this "root" layout. These child widgets, in turn, may or may not have their own hierarchy of descendants. The descendant hierarchy of a ScreenClass instance is determined at runtime by interpreting information from a text file, so I have no idea ahead of time what exactly will be put in a ScreenClass instance.

Here's my problem: I need to be able to make deep copies of any ScreenClass instance at runtime, duplicating its descendant hierarchy of widgets and layouts so that I have a complete and functional copy (except for signal connections, which I don't want to replicate right now). I have already written a copy constructor and overridden operator= for ScreenClass, and I have successfully made very rudimentary copies (right now just copying a couple of string properties), but I don't know how to take the next step of actually copying widgets and other complex objects. I've looked at QObject::children(), but I'm unsure how to make a deep copy of what it returns, or how it would be "applied" to the new copy.

Are there any general principles or techniques for making deep copies of such unpredictable and complex object hierarchies? How do I start?

Thanks for any help.

aamer4yu
14th December 2009, 04:38
I dont think you can make copies of QWidget in Qt

wysota
14th December 2009, 06:36
It's possible to make such copies but only provided that you can reproduce the state of each widget based on the list of each properties or alternatively that you provide streaming operators for your widgets and do everything manually (you'll end up doing everything manually anyway). Out of the box widgets can't be copied, as already pointed out.