2

I have multiple QObject subclasses which should act as interface classes and be implemented by (inherited by) some other classes. For example let class A : virtual public QObject and class B : virtual public QObject be interface classes. I need a QDialog object to implement their behavior like: class X: public QDialog, A, B.

Unfortunately I did not read documentation carefully at design time and now I realized two facts:

  1. implementing slots as pure virtual member functions is not possible because moc-generated code will need to call them.
  2. Multiple inheritance is not supported for QObject-derived classes. That's not a diamond thing. It's because moc-generated code can't static_cast a virtual QObject object to a A* via virtual base. (That's what compiler says!)

What is best possible alternative design to which affects code as less as possible? I can think of macro hacks. Maybe a macro in base class (like Q_OBJECT) to copy all members, signals, slots of base to derived class?

Note That's really bad that QObjects can't be inherited multiple times. Isn't?

sorush-r
  • 10,490
  • 17
  • 89
  • 173

1 Answers1

2

If you really need to expose QObject member functions through your A and B classes create an abstract base class (i.e. a class with only pure virtual member functions), say AbstractQObject, and re-declare there the QObject member functions you need to expose.

Have A and B derive virtually from AbstractQObject and X from QDialog, A and B.

This should solve the problem you described, but I suspect you would be better off redesigning your code.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • No, that's not about `QObject` members. `A` and `B` uses signals and slots, so need to inherit `QObject` and declare `Q_OBJECT` macro inside class. For example `A` have a `QFutureWatcher` object running a thread and connected its `finished` signal to one of `A`s slots. – sorush-r Mar 27 '14 at 17:20
  • Then I'm afraid your only option is to try and reorganize your code so as to use composition and delegation rather than inheritance – Nicola Musatti Mar 27 '14 at 17:38