Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multiple heritance
Qt 6.11 is out! See what's new in the release blog

Multiple heritance

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Roy44R Offline
    Roy44R Offline
    Roy44
    wrote on last edited by
    #1

    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

    kshegunovK A 2 Replies Last reply
    0
    • Roy44R Roy44

      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

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @Roy44 said in Multiple heritance:

      So is it possible ?

      No, not with QObjects.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • Roy44R Roy44

        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

        A Offline
        A Offline
        ambershark
        wrote on last edited by
        #3

        @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.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        kshegunovK 1 Reply Last reply
        2
        • M Offline
          M Offline
          mtrch
          wrote on last edited by mtrch
          #4

          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()));
          
          1 Reply Last reply
          0
          • A ambershark

            @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.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @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.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved