Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top