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
Forum Update on Monday, May 27th 2025

Multiple heritance

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.0k Views
  • 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.
  • R Offline
    R Offline
    Roy44
    wrote on 29 Jan 2017, 17:46 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

    K A 2 Replies Last reply 29 Jan 2017, 21:51
    0
    • R Roy44
      29 Jan 2017, 17:46

      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

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 29 Jan 2017, 21:51 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
      • R Roy44
        29 Jan 2017, 17:46

        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 30 Jan 2017, 03:01 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

        K 1 Reply Last reply 30 Jan 2017, 10:13
        2
        • M Offline
          M Offline
          mtrch
          wrote on 30 Jan 2017, 08:34 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
            30 Jan 2017, 03:01

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

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 30 Jan 2017, 10:13 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

            1/5

            29 Jan 2017, 17:46

            • Login

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