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. Allowing a class to inherit all my other classes
Forum Updated to NodeBB v4.3 + New Features

Allowing a class to inherit all my other classes

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 2.3k Views 1 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.
  • N Offline
    N Offline
    nicky j
    wrote on last edited by
    #1

    Hey there

    I am wondering if its possible to have a class that inherits all my other classes. imagine I have classes A, B, C, D. class A is the parent of B and C. class D is not the child of any classes, however I want class D to be accessible to all my other classes (A,B, and C) so that I can call a signal in A,B,C and have the corresponding slot be called in D and vice versa. Is this possible? Am I correct in saying that class D would have to inherit classes A,B, and C? If I do set it up this way, does that mean that in instance of class D would be accessible from classes A,B,C?
    Thanks!

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      [quote]I am wondering if its possible to have a class that inherits all my other classes[/quote]A QObject can only inherit one other QObject (although it can inherit multiple non-QObjects).

      But anyway, inheritance is not designed for the use case you've described (see http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance ).

      All you need to do is make the slots public. You can then connect the signals from A/B/C to the slots in D, and vice versa.

      @
      class A : public QObject {
      ...
      signals:
      void sig_A();
      }

      class D : public QObject {
      ...
      public slots:
      void slot_D();
      }
      @

      @
      A* a = new A;
      D* d = new D;

      QObject::connect(a, SIGNAL(sig_A()),
      d, SLOT(slot_D()));
      @

      In a nutshell, signal-slot connections need neither a parent-child relationship nor a superclass-subclass relationship.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nicky j
        wrote on last edited by
        #3

        So if I make all the slots in class D public, I can then call them from classes A,B,C regardless of parent-child relationships?

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          [quote author="nicky j" date="1392775543"]So if I make all the slots in class D public, I can then call them from classes A,B,C regardless of parent-child relationships?[/quote]Correct.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nicky j
            wrote on last edited by
            #5

            What about calling a specific instance? Say class A creates an instance of class D called Dinst. Can slots in Dinst be called from classes A,B,C (B and C are children of A). Also is it possible to create an instance of a class without that instance being a child? so have class A create and instance of class D but not have the instance of D be a child of class A.

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              [quote]What about calling a specific instance?[/quote]I'm not quite sure what you mean. All connections are made between specific instances.

              The main point I have been highlighting in recent posts (in this thread and others) is this:

              If you can obtain the pointers to any 2 QObject instances, then you can connect their signals and public slots. It doesn't matter who created the instances, what relationships they have with each other, or even which threads they live in.

              This might sound like a dumb question, but it's a genuine and important one: Do you know what pointers are?

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nicky j
                wrote on last edited by
                #7

                My understanding is that a pointer 'points' to a piece of datas memory address. I'm probably wrong though

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  [quote author="nicky j" date="1392869734"]My understanding is that a pointer 'points' to a piece of datas memory address. I'm probably wrong though[/quote]That's correct. So each (valid) QObject pointer points to an instance of a QObject.

                  Were you able to connect your unrelated objects using their pointers?

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    nicky j
                    wrote on last edited by
                    #9

                    Yes I was! thanks!

                    1 Reply Last reply
                    0

                    • Login

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