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. Interfaces must inherit QObject?!
QtWS25 Last Chance

Interfaces must inherit QObject?!

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 5 Posters 3.7k 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.
  • K KroMignon
    10 Nov 2021, 17:01

    @idlefrog said in Interfaces must inherit QObject?!:

    Thank goodness a simple inheritance reordering did the trick! :)

    I don't remember where I found this in Qt documentation (and not remember why), but QObject (or a QObject based class) must be the first class in inheritance list.

    J Offline
    J Offline
    JonB
    wrote on 10 Nov 2021, 17:04 last edited by JonB 11 Oct 2021, 17:09
    #7

    @KroMignon
    I should like a reference for this. So far as I know multiple inheritance from QObject is not supported. See for example QObject Multiple Inheritance. This is required by moc, perhaps we will see where the OP gets over time.

    The suggested solution there shows "the Qt way" of implementing what I think @idlefrog is trying to achieve, avoiding the multiple inheritance.

    I J 2 Replies Last reply 10 Nov 2021, 17:26
    1
    • J JonB
      10 Nov 2021, 17:04

      @KroMignon
      I should like a reference for this. So far as I know multiple inheritance from QObject is not supported. See for example QObject Multiple Inheritance. This is required by moc, perhaps we will see where the OP gets over time.

      The suggested solution there shows "the Qt way" of implementing what I think @idlefrog is trying to achieve, avoiding the multiple inheritance.

      I Offline
      I Offline
      idlefrog
      wrote on 10 Nov 2021, 17:26 last edited by
      #8

      @JonB Thanks for the link... it's a interesting reference. The composition solution might be handy, although looks a bit un-Qt-ish if there is such a thing! :)

      1 Reply Last reply
      0
      • J JonB
        10 Nov 2021, 17:04

        @KroMignon
        I should like a reference for this. So far as I know multiple inheritance from QObject is not supported. See for example QObject Multiple Inheritance. This is required by moc, perhaps we will see where the OP gets over time.

        The suggested solution there shows "the Qt way" of implementing what I think @idlefrog is trying to achieve, avoiding the multiple inheritance.

        J Offline
        J Offline
        JKSH
        Moderators
        wrote on 11 Nov 2021, 01:49 last edited by JKSH 11 Nov 2021, 01:50
        #9

        @KroMignon said in Interfaces must inherit QObject?!:

        I don't remember where I found this in Qt documentation (and not remember why), but QObject (or a QObject based class) must be the first class in inheritance list.

        ...

        @JonB said in Interfaces must inherit QObject?!:

        I should like a reference for this.

        https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first

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

        J 1 Reply Last reply 11 Nov 2021, 14:37
        4
        • J JKSH
          11 Nov 2021, 01:49

          @KroMignon said in Interfaces must inherit QObject?!:

          I don't remember where I found this in Qt documentation (and not remember why), but QObject (or a QObject based class) must be the first class in inheritance list.

          ...

          @JonB said in Interfaces must inherit QObject?!:

          I should like a reference for this.

          https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first

          J Offline
          J Offline
          JonB
          wrote on 11 Nov 2021, 14:37 last edited by JonB 11 Nov 2021, 14:38
          #10

          @JKSH said in Interfaces must inherit QObject?!:

          https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first

          If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.

          Thank you for this reference. However, unless I read it wrong, this does not address the @idlefrog's case or @KroMignon's solution.

          In the example in the docs

          class SomeClass : public QObject, public OtherClass
          

          Here there is no indication that the second class, OtherClass, itself inherits QObject. And it states explicitly

          Also, be sure that only the first inherited class is a QObject.

          @idlefrog is asking specifically about multiple inheritance where both classes come from QObject, and that is what i was trying to answer.

          I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve QObject is not supported, regardless of ordering?

          K K 2 Replies Last reply 11 Nov 2021, 14:53
          0
          • J JonB
            11 Nov 2021, 14:37

            @JKSH said in Interfaces must inherit QObject?!:

            https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first

            If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.

            Thank you for this reference. However, unless I read it wrong, this does not address the @idlefrog's case or @KroMignon's solution.

            In the example in the docs

            class SomeClass : public QObject, public OtherClass
            

            Here there is no indication that the second class, OtherClass, itself inherits QObject. And it states explicitly

            Also, be sure that only the first inherited class is a QObject.

            @idlefrog is asking specifically about multiple inheritance where both classes come from QObject, and that is what i was trying to answer.

            I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve QObject is not supported, regardless of ordering?

            K Offline
            K Offline
            KroMignon
            wrote on 11 Nov 2021, 14:53 last edited by
            #11

            @JonB said in Interfaces must inherit QObject?!:

            I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve QObject is not supported, regardless of ordering?

            I up-voted @JKSH post, because it gives the link to documentation I talking about.
            But, the reply to your question: according to documentation QObject multiple inheritance (diamond inheritance) is not allowed/possible.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            J 1 Reply Last reply 11 Nov 2021, 14:55
            1
            • K KroMignon
              11 Nov 2021, 14:53

              @JonB said in Interfaces must inherit QObject?!:

              I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve QObject is not supported, regardless of ordering?

              I up-voted @JKSH post, because it gives the link to documentation I talking about.
              But, the reply to your question: according to documentation QObject multiple inheritance (diamond inheritance) is not allowed/possible.

              J Offline
              J Offline
              JonB
              wrote on 11 Nov 2021, 14:55 last edited by
              #12

              @KroMignon Thanks @Kro, understood, I'm just seeking clarification because this is all getting a bit muddy.... :)

              1 Reply Last reply
              0
              • I Offline
                I Offline
                idlefrog
                wrote on 11 Nov 2021, 15:02 last edited by
                #13

                It seems to me that for a class where you inherited perhaps a set of interfaces and one QObject, you can't inherit any other QObject class. e.g.

                class RealCake: public QObject, public ICake, public IEdible{}
                
                class PlasticCake: public QObject, public ICake {}
                
                class JokeCake: public PlasticCake, public RealCake{}
                

                The JokeCake is not allowed in the world of Qt. Pity!

                K J 2 Replies Last reply 11 Nov 2021, 15:11
                0
                • J JonB
                  11 Nov 2021, 14:37

                  @JKSH said in Interfaces must inherit QObject?!:

                  https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first

                  If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.

                  Thank you for this reference. However, unless I read it wrong, this does not address the @idlefrog's case or @KroMignon's solution.

                  In the example in the docs

                  class SomeClass : public QObject, public OtherClass
                  

                  Here there is no indication that the second class, OtherClass, itself inherits QObject. And it states explicitly

                  Also, be sure that only the first inherited class is a QObject.

                  @idlefrog is asking specifically about multiple inheritance where both classes come from QObject, and that is what i was trying to answer.

                  I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve QObject is not supported, regardless of ordering?

                  K Away
                  K Away
                  kshegunov
                  Moderators
                  wrote on 11 Nov 2021, 15:10 last edited by kshegunov 11 Nov 2021, 15:13
                  #14

                  @JonB said in Interfaces must inherit QObject?!:

                  I see that several members, including @kshegunov, have up-voted your post. I would welcome clarification on this issue, please, as my understand is that multiple inheritance where both involve QObject is not supported, regardless of ordering?

                  If both classes inherit from QObject you enter virtual-inheritance-land, where the night is dark and full of terrors ... and it's simply not supported by moc (last sentence of the mentioned link). Otherwise multiple inheritance is fine, good, expected and so on (provided you use it properly), and is supported both by the language and by Qt with the minor note about base class order.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • I idlefrog
                    11 Nov 2021, 15:02

                    It seems to me that for a class where you inherited perhaps a set of interfaces and one QObject, you can't inherit any other QObject class. e.g.

                    class RealCake: public QObject, public ICake, public IEdible{}
                    
                    class PlasticCake: public QObject, public ICake {}
                    
                    class JokeCake: public PlasticCake, public RealCake{}
                    

                    The JokeCake is not allowed in the world of Qt. Pity!

                    K Away
                    K Away
                    kshegunov
                    Moderators
                    wrote on 11 Nov 2021, 15:11 last edited by
                    #15

                    @idlefrog said in Interfaces must inherit QObject?!:

                    The JokeCake is not allowed in the world of Qt. Pity!

                    Even if it were allowed, you're using it incorrectly. C++ is hard, man, it's just the way it is.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • I idlefrog
                      11 Nov 2021, 15:02

                      It seems to me that for a class where you inherited perhaps a set of interfaces and one QObject, you can't inherit any other QObject class. e.g.

                      class RealCake: public QObject, public ICake, public IEdible{}
                      
                      class PlasticCake: public QObject, public ICake {}
                      
                      class JokeCake: public PlasticCake, public RealCake{}
                      

                      The JokeCake is not allowed in the world of Qt. Pity!

                      J Offline
                      J Offline
                      JonB
                      wrote on 11 Nov 2021, 15:14 last edited by
                      #16

                      @idlefrog
                      Yes indeed that is precisely my understanding (unless mistaken). You must not inherit multiple times from QObejct, whether directly or indirectly, and regardless of order. Because of moc requirement/limitation.

                      I still refer you back to QObject Multiple Inheritance. Ah, hang on, I think the solution I had in mind is at https://stackoverflow.com/a/18113601/489865 "the Qt way of doing this is like this...." But now I see that includes

                      I am amazed that this actually works, but bummed out by the fact that it requires the old-style SIGNAL()/SLOT() syntax...

                      [Not my language!] Which is a bu**er :(

                      K 1 Reply Last reply 11 Nov 2021, 15:16
                      0
                      • J JonB
                        11 Nov 2021, 15:14

                        @idlefrog
                        Yes indeed that is precisely my understanding (unless mistaken). You must not inherit multiple times from QObejct, whether directly or indirectly, and regardless of order. Because of moc requirement/limitation.

                        I still refer you back to QObject Multiple Inheritance. Ah, hang on, I think the solution I had in mind is at https://stackoverflow.com/a/18113601/489865 "the Qt way of doing this is like this...." But now I see that includes

                        I am amazed that this actually works, but bummed out by the fact that it requires the old-style SIGNAL()/SLOT() syntax...

                        [Not my language!] Which is a bu**er :(

                        K Away
                        K Away
                        kshegunov
                        Moderators
                        wrote on 11 Nov 2021, 15:16 last edited by kshegunov 11 Nov 2021, 15:24
                        #17

                        @JonB said in Interfaces must inherit QObject?!:

                        I still refer you back to QObject Multiple Inheritance. Ah, hang on, I think the solution I had in mind is at https://stackoverflow.com/a/18113601/489865 "the Qt way of doing this is like this...." But now I see that includes

                        This is simply an abomination. Signals act polymorphically without the need to declare them as pure virtual in interfaces. Slots are a different kettle of fish, they work just fine with overriding.

                        (Edit)
                        Addendum:

                        Yes indeed that is precisely my understanding (unless mistaken). You must not inherit multiple times from QObejct, whether directly or indirectly, and regardless of order. Because of moc requirement/limitation.

                        Just to spill it as explicitly and bluntly as I possibly can:
                        You shall not derive a class from the same base multiple times (directly or indirectly) if you don't intimately know what's the difference between:

                        class Derived : public Base
                        

                        and

                        class Derived : virtual public Base
                        

                        Read and abide by the Qt Code of Conduct

                        J 1 Reply Last reply 11 Nov 2021, 15:24
                        3
                        • K kshegunov
                          11 Nov 2021, 15:16

                          @JonB said in Interfaces must inherit QObject?!:

                          I still refer you back to QObject Multiple Inheritance. Ah, hang on, I think the solution I had in mind is at https://stackoverflow.com/a/18113601/489865 "the Qt way of doing this is like this...." But now I see that includes

                          This is simply an abomination. Signals act polymorphically without the need to declare them as pure virtual in interfaces. Slots are a different kettle of fish, they work just fine with overriding.

                          (Edit)
                          Addendum:

                          Yes indeed that is precisely my understanding (unless mistaken). You must not inherit multiple times from QObejct, whether directly or indirectly, and regardless of order. Because of moc requirement/limitation.

                          Just to spill it as explicitly and bluntly as I possibly can:
                          You shall not derive a class from the same base multiple times (directly or indirectly) if you don't intimately know what's the difference between:

                          class Derived : public Base
                          

                          and

                          class Derived : virtual public Base
                          
                          J Offline
                          J Offline
                          JonB
                          wrote on 11 Nov 2021, 15:24 last edited by JonB 11 Nov 2021, 15:28
                          #18

                          @kshegunov
                          Yeah, I didn't notice the virtual on the signal. That's the trouble with assuming an accepted solution with 60 up-votes must be good!

                          It shows a lot of people are obviously struggling with this. How then can the OP here arrange to write an "interface" which inherits from QObject and apply it to classes which inherit from QObject too?

                          Ohhhh, maybe I misunderstood? Only now do I see that OP did not originally say that ICake needed QObject itself. Only that he put it first in the class inheritance list before QObject and that upset moc. I get it now, and why public QObject, public ICake works here. OK, but what about if OP did want multiple QObject inheritance, any way without the "abomination" ?

                          K 1 Reply Last reply 11 Nov 2021, 15:25
                          0
                          • J JonB
                            11 Nov 2021, 15:24

                            @kshegunov
                            Yeah, I didn't notice the virtual on the signal. That's the trouble with assuming an accepted solution with 60 up-votes must be good!

                            It shows a lot of people are obviously struggling with this. How then can the OP here arrange to write an "interface" which inherits from QObject and apply it to classes which inherit from QObject too?

                            Ohhhh, maybe I misunderstood? Only now do I see that OP did not originally say that ICake needed QObject itself. Only that he put it first in the class inheritance list before QObject and that upset moc. I get it now, and why public QObject, public ICake works here. OK, but what about if OP did want multiple QObject inheritance, any way without the "abomination" ?

                            K Away
                            K Away
                            kshegunov
                            Moderators
                            wrote on 11 Nov 2021, 15:25 last edited by
                            #19

                            @JonB said in Interfaces must inherit QObject?!:

                            OK, but what about if OP did want multiple QObject inheritnace, any way without the "abomination" ?

                            He shan't do it. Also read the addendum I added to my previous post.

                            Read and abide by the Qt Code of Conduct

                            J 1 Reply Last reply 11 Nov 2021, 15:32
                            0
                            • K kshegunov
                              11 Nov 2021, 15:25

                              @JonB said in Interfaces must inherit QObject?!:

                              OK, but what about if OP did want multiple QObject inheritnace, any way without the "abomination" ?

                              He shan't do it. Also read the addendum I added to my previous post.

                              J Offline
                              J Offline
                              JonB
                              wrote on 11 Nov 2021, 15:32 last edited by
                              #20

                              @kshegunov
                              Is that a requirement of C++? The OP claimed

                              class VictoriaSpongeCake : public ICake, public QObject
                              

                              So if I change ICake to inherit like this:

                              class ICake: public QObject
                              

                              It all compiles without a problem.

                              That is not quoting me, it is quoting him.

                              K J 2 Replies Last reply 11 Nov 2021, 15:36
                              0
                              • J JonB
                                11 Nov 2021, 15:32

                                @kshegunov
                                Is that a requirement of C++? The OP claimed

                                class VictoriaSpongeCake : public ICake, public QObject
                                

                                So if I change ICake to inherit like this:

                                class ICake: public QObject
                                

                                It all compiles without a problem.

                                That is not quoting me, it is quoting him.

                                K Away
                                K Away
                                kshegunov
                                Moderators
                                wrote on 11 Nov 2021, 15:36 last edited by kshegunov 11 Nov 2021, 15:36
                                #21

                                @JonB said in Interfaces must inherit QObject?!:

                                Is that a requirement of C++?

                                It is not a requirement, but there's a significant difference.
                                Here's links, I know you love them sources:
                                https://isocpp.org/wiki/faq/multiple-inheritance (What is the “dreaded diamond”? being key)
                                https://en.cppreference.com/w/cpp/language/derived_class (Virtual base classes is relevant)

                                Read and abide by the Qt Code of Conduct

                                J 1 Reply Last reply 11 Nov 2021, 15:43
                                3
                                • K kshegunov
                                  11 Nov 2021, 15:36

                                  @JonB said in Interfaces must inherit QObject?!:

                                  Is that a requirement of C++?

                                  It is not a requirement, but there's a significant difference.
                                  Here's links, I know you love them sources:
                                  https://isocpp.org/wiki/faq/multiple-inheritance (What is the “dreaded diamond”? being key)
                                  https://en.cppreference.com/w/cpp/language/derived_class (Virtual base classes is relevant)

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 11 Nov 2021, 15:43 last edited by
                                  #22

                                  @kshegunov
                                  Yeah, thanks, this looks good. public virtual Base, that's a new one for me, I like it. The more complexity in C++ the better... ;-)

                                  1 Reply Last reply
                                  1
                                  • J JonB
                                    11 Nov 2021, 15:32

                                    @kshegunov
                                    Is that a requirement of C++? The OP claimed

                                    class VictoriaSpongeCake : public ICake, public QObject
                                    

                                    So if I change ICake to inherit like this:

                                    class ICake: public QObject
                                    

                                    It all compiles without a problem.

                                    That is not quoting me, it is quoting him.

                                    J Offline
                                    J Offline
                                    JKSH
                                    Moderators
                                    wrote on 12 Nov 2021, 02:50 last edited by JKSH 11 Dec 2021, 02:56
                                    #23

                                    @JonB said in Interfaces must inherit QObject?!:

                                    @kshegunov
                                    Is that a requirement of C++? The OP claimed

                                    class VictoriaSpongeCake : public ICake, public QObject
                                    

                                    So if I change ICake to inherit like this:

                                    class ICake: public QObject
                                    

                                    It all compiles without a problem.

                                    That is not quoting me, it is quoting him.

                                    OP forgot to re-run qmake (or delete the build directory) after making ICake inherit QObject and adding the Q_OBJECT macro.

                                    @JonB said in Interfaces must inherit QObject?!:

                                    How then can the OP here arrange to write an "interface" which inherits from QObject and apply it to classes which inherit from QObject too?

                                    They can't.

                                    QObjects are designed to be "identity objects", as opposed to "value objects": https://doc.qt.io/qt-5/object.html#qt-objects-identity-vs-value (for example, an image is a value but a window is an identity). I'd say that an "interface object" is neither an identity nor a value.

                                    One more thought: If a class inherits from 2 differnent QObjects, what should obj->staticMetaObject.superClass() return?

                                    P.S. I think you'll enjoy the thoroughness of this article: https://www.ics.com/blog/multiple-inheritance-qt

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

                                    J 1 Reply Last reply 12 Nov 2021, 07:03
                                    2
                                    • J JKSH
                                      12 Nov 2021, 02:50

                                      @JonB said in Interfaces must inherit QObject?!:

                                      @kshegunov
                                      Is that a requirement of C++? The OP claimed

                                      class VictoriaSpongeCake : public ICake, public QObject
                                      

                                      So if I change ICake to inherit like this:

                                      class ICake: public QObject
                                      

                                      It all compiles without a problem.

                                      That is not quoting me, it is quoting him.

                                      OP forgot to re-run qmake (or delete the build directory) after making ICake inherit QObject and adding the Q_OBJECT macro.

                                      @JonB said in Interfaces must inherit QObject?!:

                                      How then can the OP here arrange to write an "interface" which inherits from QObject and apply it to classes which inherit from QObject too?

                                      They can't.

                                      QObjects are designed to be "identity objects", as opposed to "value objects": https://doc.qt.io/qt-5/object.html#qt-objects-identity-vs-value (for example, an image is a value but a window is an identity). I'd say that an "interface object" is neither an identity nor a value.

                                      One more thought: If a class inherits from 2 differnent QObjects, what should obj->staticMetaObject.superClass() return?

                                      P.S. I think you'll enjoy the thoroughness of this article: https://www.ics.com/blog/multiple-inheritance-qt

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 12 Nov 2021, 07:03 last edited by
                                      #24

                                      @JKSH said in Interfaces must inherit QObject?!:

                                      OP forgot to re-run qmake (or delete the build directory) after making ICake inherit QObject and adding the Q_OBJECT macro.

                                      They can't.

                                      Thank you, this makes a lot more sense then!

                                      1 Reply Last reply
                                      1

                                      16/24

                                      11 Nov 2021, 15:14

                                      • Login

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