1

in my Qt multithread programm I want to implement a QObject-based baseclass, so that every class derived from it cann use its signals and slots (for example to throw an error).

I implemented MyQObject : public QObject{...}. But for classes that derive from QWidget I cannot multi inher from QWidget and MyQObject.

I can solve the problem by calling the slot directly by QMetaObject::invokeMethod(...), but i am interested if there would be another way to solve this problem.

Louis Kröger
  • 344
  • 3
  • 10
  • 1
    `QWidget` already derives from `QObject`. You don't need to inherit `QObject` again. In other words, you can use signals/slots in your `QWidget` sub classes. – vahancho Feb 15 '18 at 14:53
  • That is the problem. I want to add my own signals in a "global" base-class. – Louis Kröger Feb 15 '18 at 14:55
  • What are the use-cases of such global signals or slots ? Maybe you could change the design and not use signal/slots for what you want to achieve ? Like having a composition instead of inheritance and directly call what your need on the `MyQObjectAsComposition`. – ymoreau Feb 15 '18 at 15:00
  • This would be a solid solution in my case. I guess i will think about this. But this question is more general. Someone may need a Signal-Base-Class/Interface. – Louis Kröger Feb 15 '18 at 15:26
  • 1
    Related: https://stackoverflow.com/questions/12131357/multiple-inheritance-with-qobject-base?rq=1 – Rama Feb 15 '18 at 15:38

1 Answers1

0

Multiple inheritance with QObject can be done following these rules:

  1. Only one of the parents class can be a QObject/QWidget

  2. The QObject parent have to be the first parent in the initialization.

There's a trick that allows you to declare a signal like function in a non-qobject interface and then declaring it inheriting that interface using composition. Take a look to this post, it may be useful.

Moia
  • 2,216
  • 1
  • 12
  • 34