Multiple heritance



  • Hi,

    This question has probably been asked 10000000th times.
    I would like to do something like this:

    class EditorBase : public QObject
    {
      Q_OBJECT
      //some code
      signals:
         void sg_mySignal();
    
      public slots:
         void onSlot();
    };
    
    class MyEditor : public QLineEdit, public EditorBase
    {
       Q_OBJECT
       // code
    };
    

    I saw examples with Q_DECLARE_INTERFACE but interfaces do not contain signals or slot.
    So is it possible ? ans How.

    Thanks,
    sorry for my horrible english


  • Qt Champions 2016

    @Roy44 said in Multiple heritance:

    So is it possible ?

    No, not with QObjects.



  • @Roy44 Multiple inheritance is rarely a good idea. In 20 years of C++ I think I have used it once.

    There is usually a better way than using multiple inheritance. In this case you could pass in a QLineEdit or just create one as a member of MyEditor.



  • In this case you probably don't need EditorBase class, because signals & slots are connected dynamically, using Qt meta-object system, so you can create connections passing base QObject* pointer as argument:

    QObject *editor = new MyEditor(parent);
    //QLineEdit signals & slots:
    connect(editor, SIGNAL(textChanged(QString)), SLOT(someSlot(QString)));
    //MyEditor signals & slots
    connect(editor, SIGNAL(sg_mySignal()), SLOT(otherSlot()));
    connect(button, SIGNAL(clicked()), editor, SLOT(onSlot()));
    

  • Qt Champions 2016

    @ambershark said in Multiple heritance:

    Multiple inheritance is rarely a good idea. In 20 years of C++ I think I have used it once.

    Funnily, I have a similar story. ;)

    @Roy44
    As @ambershark suggested the usual thing to do in this case is to aggregate the line edit (i.e. have it as a member) and if necessary to forward its signals.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.