This post is because of QQ15.

I have an existing application which has several object, all related, but not yet using a single unifying object to command a universal interface across these objects. There are 4 different main types, one table object (which inherits QTable), one graph object (which inherits QWidget) and two text objects (which both inherit QTextView).
All these object thusly have a common ancestor, namely QWidget.
Using single inheritance would be a solution, but would require the reimplementing of Qt code, which would make it harder to maintain and which should be needless in the first place.

As far as I can read from the article, the solution is to create an interface and a wrapper, as follows:

Qt Code:
  1. class Interface {
  2. public:
  3. virtual void method() = 0;
  4. };
  5.  
  6. class Wrapper : public QWidget, public Interface {
  7. Q_OBJECT
  8. Interface *wrapped;
  9. public:
  10. Wrapper(QWidget *p) : QWidget(p) {
  11. wrapped = cast_stuff<Interface *>(p);
  12. }
  13. ~Wrapper();
  14. public slots:
  15. void method() { wrapped->method(); }
  16. };
To copy to clipboard, switch view to plain text mode 

Then you create an object structure based on the interface:

Qt Code:
  1. class Document : public QWidget, public Interface {
  2. Q_OBJECT // Is this even necessary?
  3. Wrapper *wrapper;
  4. public:
  5. Document(QWidget *p = 0) : QWidget(p) { }
  6. virtual ~Document() { }
  7. virtual void method() { generic_implementation; }
  8. };
  9.  
  10. class Table : public QTable, public Document {
  11. public:
  12. Table(QWidget *p = 0) : QTable(p), Document(0) {
  13. }
  14. ~Table() {
  15. }
  16. virtual void method() { specific_implementation; }
  17. };
To copy to clipboard, switch view to plain text mode 


This is completely based on the example declarations. Now I have a few questions.
First of all, how would you go about creating a specific object?
Is it like this:
Qt Code:
  1. Interface *d = new Table();
To copy to clipboard, switch view to plain text mode 
(But then you wouldn't need the wrapper?)
Or would you use the wrapper, like so:
Qt Code:
  1. Wrapper *w = new Wrapper(new Table());
To copy to clipboard, switch view to plain text mode 
Second, why would you include the Wrapper object in the Document, does the wrapped object need to know if it's being wrapped and if so, why?

I hope someone can help me with the problem, as Voker's example code didn't include any usage tips.