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

Multiple inheritance of QObject

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 508 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
    Rua3n
    wrote on 12 Jul 2023, 19:00 last edited by
    #1

    I'm trying to create a class in which all top-level widgets of my application could inherit from it, so I could define all Q_PROPERTY that I would like to expose in just this class instead of declaring it in each different class.

    I'm getting compilation errors about ambiguous ~ because QMainWindow already inherits QObject.

    What i can do in this case?

    class WindowProperty : public QObject
    {
        Q_OBJECT
    	Q_PROPERTY(int test MEMBER test WRITE setTest)
    public:
        int test = 0;
        void setTest(int xtest)
        {
            qDebug() << "xtest:" << xtest;
            qDebug() << "test:" << test;
        }
    };
    
    
    class MainWindow : public QMainWindow, public WindowProperty
    {
        Q_OBJECT
    public:
    }
    
    P 1 Reply Last reply 12 Jul 2023, 19:08
    0
    • R Rua3n
      12 Jul 2023, 19:00

      I'm trying to create a class in which all top-level widgets of my application could inherit from it, so I could define all Q_PROPERTY that I would like to expose in just this class instead of declaring it in each different class.

      I'm getting compilation errors about ambiguous ~ because QMainWindow already inherits QObject.

      What i can do in this case?

      class WindowProperty : public QObject
      {
          Q_OBJECT
      	Q_PROPERTY(int test MEMBER test WRITE setTest)
      public:
          int test = 0;
          void setTest(int xtest)
          {
              qDebug() << "xtest:" << xtest;
              qDebug() << "test:" << test;
          }
      };
      
      
      class MainWindow : public QMainWindow, public WindowProperty
      {
          Q_OBJECT
      public:
      }
      
      P Offline
      P Offline
      Pl45m4
      wrote on 12 Jul 2023, 19:08 last edited by Pl45m4 7 Dec 2023, 19:08
      #2

      @Rua3n

      Why not include the properties instead of inherit from them?
      "Making" QMainWindow "a property" (-> inheritance = "is a") doesn't make too much sense IMO


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      R 1 Reply Last reply 12 Jul 2023, 20:11
      0
      • P Pl45m4
        12 Jul 2023, 19:08

        @Rua3n

        Why not include the properties instead of inherit from them?
        "Making" QMainWindow "a property" (-> inheritance = "is a") doesn't make too much sense IMO

        R Offline
        R Offline
        Rua3n
        wrote on 12 Jul 2023, 20:11 last edited by Rua3n 7 Dec 2023, 20:13
        #3

        @Pl45m4 how to include Q_PROPERTIES?
        What doesnt make sense? if you have 10 different classes that you want to share the same Q_PROPERTIEs, why keep track of defining all Q_PROPERTIES in each of these classes instead of just one?

        P 1 Reply Last reply 12 Jul 2023, 23:10
        0
        • R Rua3n
          12 Jul 2023, 20:11

          @Pl45m4 how to include Q_PROPERTIES?
          What doesnt make sense? if you have 10 different classes that you want to share the same Q_PROPERTIEs, why keep track of defining all Q_PROPERTIES in each of these classes instead of just one?

          P Offline
          P Offline
          Pl45m4
          wrote on 12 Jul 2023, 23:10 last edited by
          #4

          @Rua3n

          If you really want to do this, you could try to make WindowProperty a Q_GADGET.
          This doesn't allow signals and slots in there, since it's a light version of Q_OBJECT, but you can still use Q_PROPERTY from the MOC system.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          R 1 Reply Last reply 12 Jul 2023, 23:22
          0
          • P Pl45m4
            12 Jul 2023, 23:10

            @Rua3n

            If you really want to do this, you could try to make WindowProperty a Q_GADGET.
            This doesn't allow signals and slots in there, since it's a light version of Q_OBJECT, but you can still use Q_PROPERTY from the MOC system.

            R Offline
            R Offline
            Rua3n
            wrote on 12 Jul 2023, 23:22 last edited by
            #5

            @Pl45m4 I already had tried it, i get the same error using Q_GADGET

            C 1 Reply Last reply 13 Jul 2023, 03:36
            0
            • R Rua3n
              12 Jul 2023, 23:22

              @Pl45m4 I already had tried it, i get the same error using Q_GADGET

              C Offline
              C Offline
              ChrisW67
              wrote on 13 Jul 2023, 03:36 last edited by
              #6

              It compiles just fine with Q_GADGET but it still will not do what you are expecting. The code generated by MOC is not heritable. (It's also in the WindowProperty private section where it would not be visible anyway. Changing that does not help.)

              #include <QApplication>
              #include <QMainWindow>
              #include <QDebug>
              
              class WindowProperty {  // not a QObject
                  Q_GADGET
                  Q_PROPERTY(int test MEMBER test WRITE setTest)
              public:
                  int test = 0;
                  void setTest(int xtest)  {
                      qDebug() << "xtest:" << xtest;
                      qDebug() << "test:" << test;
                  }
              };
              
              class MainWindow : public QMainWindow, public WindowProperty
              {
                  Q_OBJECT
              public:
              };
              
              int main(int argc, char **argv) {
                      QApplication app(argc, argv);
                      MainWindow w;
                      qDebug() << w.property("test");  // invalid variant, not 0
                      w.setProperty("test", 42); // sets a property in w's QObject
                      qDebug() << w.property("test"); // 42 , not 0
                      return app.exec();
              }
              #include "main.moc"
              
              R 1 Reply Last reply 13 Jul 2023, 04:58
              0
              • C ChrisW67
                13 Jul 2023, 03:36

                It compiles just fine with Q_GADGET but it still will not do what you are expecting. The code generated by MOC is not heritable. (It's also in the WindowProperty private section where it would not be visible anyway. Changing that does not help.)

                #include <QApplication>
                #include <QMainWindow>
                #include <QDebug>
                
                class WindowProperty {  // not a QObject
                    Q_GADGET
                    Q_PROPERTY(int test MEMBER test WRITE setTest)
                public:
                    int test = 0;
                    void setTest(int xtest)  {
                        qDebug() << "xtest:" << xtest;
                        qDebug() << "test:" << test;
                    }
                };
                
                class MainWindow : public QMainWindow, public WindowProperty
                {
                    Q_OBJECT
                public:
                };
                
                int main(int argc, char **argv) {
                        QApplication app(argc, argv);
                        MainWindow w;
                        qDebug() << w.property("test");  // invalid variant, not 0
                        w.setProperty("test", 42); // sets a property in w's QObject
                        qDebug() << w.property("test"); // 42 , not 0
                        return app.exec();
                }
                #include "main.moc"
                
                R Offline
                R Offline
                Rua3n
                wrote on 13 Jul 2023, 04:58 last edited by
                #7

                @ChrisW67 im trying to inherit the Q_PROPERTIES from WindowProperty and use them in the MainWindow Isn't possible?

                1 Reply Last reply
                0

                1/7

                12 Jul 2023, 19:00

                • Login

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