The best way to extend QGraphicsItem
I want to extend QGraphicsItem to add "QString toXML()" and "void fromXML(QString xml)" to all of my custom GraphicsItem widgets. What is the best way to this? The reason for wanting to this, is to iterate through all of my GraphicsScene and save the graphicsScene and restore the graphicsScene. The way I have to do this now is to determine the type and then cast the item to the right widget object. However, it would be much nicer not to determine the type and to cast.
I was thinking, maybe the best way is to make my own base Class that inherits QGraphicsItem. However my custom widgets don't directly inherit QGraphicsItem but rather QGraphicsEllipseItem, QGraphicsRectItem, QGraphicsPolygonItem...etc. Any suggestions?
I have found two possible ways:
Create a wrapper base class derived from QGraphicsItem (or QGraphicsObject or QGraphicsWidget), which contains the items you currently use. Your current items become child items to this new (invisible) class.
The class need not stay invisible, though. It could implement common drawing behavior and common user interaction behavior.
Create an abstract base class, and let your items derive from both QGraphics* and that abstract class. Here, you have to take care that you do not run into any naming conflicts with the existing hierarchy of QGraphics* classes. If you want to share code between your classes, this doesn't really help you, but it does give you a clean interface to access all your classes with.
But "Inheritance is not for code reuse anyway":http://www.parashift.com/c++-faq/smalltalk-and-inherit.html