0

I have an interface class in C++ that consists of pure virtual member functions, which I want to make parent of all the QWidgets in my app. It has lots of signals and slots, so it must inherit from QObject. The problem is that I can't make my other objects derive from this:

class Interface : public QObject {

    public slots:
        virtual void slotA() = 0;
    signals:
        void signalA();

};

Now if I want to create a widget implementing this interface I would need to do something like

class MyWidget : public QWidget, public Interface {

};

This however can't be done because it can't inherit twice from QObject. How can I do this?

Note: QObject Multiple Inheritance does not answer to this as it does not post a satisfatory answer in my opinion

Community
  • 1
  • 1
manatttta
  • 3,054
  • 4
  • 34
  • 72
  • 1
    To use signals and slots, the function doesn't have to be statically known. Using the old `SIGNAL` and `SLOT` macros, you can rely on runtime-detection of the appropriate signals and slots in the objects. If you still want to call `->slotA();`, you can make it a normal non-slot function in the interface. Qt slots can implement pure virtual functions just fine. – Johannes Schaub - litb Mar 25 '17 at 16:42
  • I know, but I would have to redeclare ALL signals and slots from base `interface` in my children classes. Else, I would get lots of errors like "signal not found" etc – manatttta Mar 25 '17 at 16:44
  • 3
    The easiest workaround is to put the interface as a member of your widgets, and make clients connect to the member's signals exposed as `getInterfaceObject()`. – Johannes Schaub - litb Mar 25 '17 at 16:50
  • why don't you have a class `BaseWidget` that inherits from `QWidget` and has all the signals/slots you want? After that, You can make your widgets inherit from that `BaseWidget` (since you state that you want to *make it the parent of all the **`QWidget`s** in your app*). This can work around the issue, unless you have non-`QWidget`s that should inherit that interface. If this is the case, @JohannesSchaub-litb 's last comment provides another workaround. – Mike Mar 25 '17 at 16:58
  • @Mike by `QWidget` I also meant its children, for instance `QDockWidget` – manatttta Mar 25 '17 at 17:11
  • Possible duplicate of [QObject Multiple Inheritance](http://stackoverflow.com/questions/8578657/qobject-multiple-inheritance) – cbuchart Mar 27 '17 at 14:19
  • @JohannesSchaub-litb I opted for that solution, care to post an answer? – manatttta Mar 27 '17 at 15:56
  • @manatttta If you used that solution successfully, why not post the answer yourself and accept it, too? – Martin Hennings Nov 24 '17 at 07:49

0 Answers0