c++qtqt5

Qt: Inherit QObject in parent and other QWidget subclass in child


I am writing a series of custom classes for Qt. What I need is to have a base class that has few custom signals and slots, and children classes will have it. However, I do know that inheriting from the same classes will be thrown an error. I have read the documentation but only dictates that I need to include QObject if I wish to use Q_OBJECT macro. This is the following sample code that I intend to do:

class Base : public QObject
{
Q_OBJECT

base signals here

public base slots here
}

class Child : public QLabel, public Base 
{
// Other codes here
}

Will it be possible this way? Since I only wish to use Qt to connect with all children inherits from the parent class.


Solution

  • Multiple inheritance from QObject will not work for several reasons. (Moc-Compiler does not work correctly and QObject has member variables which makes multiple inheritance unsafe).

    You better work with composition instead of inheritance in case you want to provide the same behaviour to several objects.