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. [Solved] Subclassing from QObject base and non Qobject base classes
Forum Updated to NodeBB v4.3 + New Features

[Solved] Subclassing from QObject base and non Qobject base classes

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 3.7k 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.
  • M Offline
    M Offline
    moravas
    wrote on 30 Aug 2014, 08:00 last edited by
    #1

    Hi Folks,

    I have the following architecture:

    @
    class nativeBase {
    protected: nativeBase();
    ... some stuff...
    }

    class nativeDerived: public nativeBase {
    protected: nativeDerived();
    ... some stuff...
    }
    @

    Please consider the protected constructor due to prevent the anauthorized creation, and the fact, that there is no Qt specific stuff, like signals or slots...

    @
    class qobjectBase: public QObject {
    Q_OBJECT

    ... some Qt signals and slots...
    protected: qobjectBase();
    ... some stuff...
    }
    @

    Lets consider the protected constructor, as above and there are some signals and / or slots.

    @
    class surface: public qobjectBase, public nativeDerived {
    Q_OBJECT
    }
    @

    During compilation, I get an error, that there are no qt_metatype(), and other methods which are included by the Q_OBJECT macro whitin the nativeDerived class. It is true, because I don't introduce it. I investigated that I derived the nativeBase from QObject, but in this case, I get an error, that multiple inheritance from QObject is prohibited.

    The question is: how can I solve this issue? Maybe, is any solution to tell to Qt, that this is not needed to iterate over the "native" inheritance chain?

    Regards,
    Norbert

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jeremy_k
      wrote on 30 Aug 2014, 19:52 last edited by
      #2

      Can you provide more details? This test based on your example builds and runs for me:

      @#include <QObject>
      #include <cstdio>

      class nativeBase {
      protected: nativeBase(){}
      };

      class nativeDerived: public nativeBase {
      protected: nativeDerived(){}
      };

      class qobjectBase: public QObject {
      Q_OBJECT

      protected: qobjectBase(){}
      };

      class surface: public qobjectBase, public nativeDerived {
      Q_OBJECT
      signals:
      void sig();
      public slots:
      void slot(){ printf("slot!\n"); }
      };

      int main(int argc, char *argv[])
      {
      Q_UNUSED(argc);
      Q_UNUSED(argv);
      surface s;
      QObject::connect(&s,&surface::sig, &s, &surface::slot);
      emit s.sig();
      return 0;
      }

      #include "main.moc"@

      Regarding deriving a class from multiple classes that inherit QObject, that's the diamond inheritance problem. http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 30 Aug 2014, 21:19 last edited by
        #3

        Hi,

        The diamond problem is not really applicable here since you can't inherit multiple times from QObject.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          moravas
          wrote on 31 Aug 2014, 10:50 last edited by
          #4

          [quote author="jeremy_k" date="1409428351"]Can you provide more details? This test based on your example builds and runs for me:

          @#include <QObject>
          #include <cstdio>

          class nativeBase {
          protected: nativeBase(){}
          };

          class nativeDerived: public nativeBase {
          protected: nativeDerived(){}
          };

          class qobjectBase: public QObject {
          Q_OBJECT

          protected: qobjectBase(){}
          };

          class surface: public qobjectBase, public nativeDerived {
          Q_OBJECT
          signals:
          void sig();
          public slots:
          void slot(){ printf("slot!\n"); }
          };

          int main(int argc, char *argv[])
          {
          Q_UNUSED(argc);
          Q_UNUSED(argv);
          surface s;
          QObject::connect(&s,&surface::sig, &s, &surface::slot);
          emit s.sig();
          return 0;
          }

          #include "main.moc"@

          Regarding deriving a class from multiple classes that inherit QObject, that's the diamond inheritance problem. http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem[/quote]

          Hi,

          I think, you are a bit confused: there are no signals and / or slots whitin the surface class. The signals and / or slots are only defined and implemented in the qobjectBase class, but in order to use their in connect() method, I use those via the surface class.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            moravas
            wrote on 31 Aug 2014, 10:51 last edited by
            #5

            [quote author="SGaist" date="1409433572"]Hi,

            The diamond problem is not really applicable here since you can't inherit multiple times from QObject.[/quote]

            Hi,

            yes, you are right, it is not an exactly diamond problem

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jeremy_k
              wrote on 31 Aug 2014, 18:34 last edited by
              #6

              [quote author="moravas" date="1409482210"]
              Hi,

              I think, you are a bit confused: there are no signals and / or slots whitin the surface class. The signals and / or slots are only defined and implemented in the qobjectBase class, but in order to use their in connect() method, I use those via the surface class.[/quote]

              Great. So post an example that demonstrates your problem.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • M Offline
                M Offline
                moravas
                wrote on 1 Sept 2014, 11:23 last edited by
                #7

                Hi,

                this is the concrete architecture:
                @
                class nativeBase {
                public: virtual ~nativeBase();

                protected: nativeBase();

                protected: QSettings _cfg;
                };

                class nativeDerived: public nativeBase {

                public: virtual ~nativeDerived();

                protected: nativeDerived();

                private: QString _processName;
                };

                class qobjectBase: public QObject {
                Q_OBJECT

                public: virtual ~qobjectBase();

                signals: void sigChanged();

                public slots: void onNew(const QString& name);
                public slots: void onOpen(const QString& name);
                public slots: void onDelete(const QString& name);
                public slots: void onSaveAs(const QString& name);

                protected: qobjectBase();
                };

                class surface: public nativeDerived, public qobjectBase {
                Q_OBJECT
                };
                @

                Regards,
                Norbert

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jeremy_k
                  wrote on 1 Sept 2014, 11:40 last edited by
                  #8

                  Moc assumes that the QObject derived class comes first in the order of inheritance.

                  http://qt-project.org/doc/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    moravas
                    wrote on 3 Sept 2014, 05:43 last edited by
                    #9

                    Hi,

                    thanks a lot!!! It seems to be helpful.

                    Bye

                    1 Reply Last reply
                    0

                    1/9

                    30 Aug 2014, 08:00

                    • Login

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