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] MetaObject not referencing slots in derived class

[solved] MetaObject not referencing slots in derived class

Scheduled Pinned Locked Moved Unsolved General and Desktop
metadataslots
16 Posts 3 Posters 4.8k 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.
  • M mrjj
    18 Aug 2016, 12:24

    Hi
    I wonder if Derived class need

    Q_OBJECT

    too ?

    K Offline
    K Offline
    kshegunov
    Moderators
    wrote on 18 Aug 2016, 12:35 last edited by
    #3

    @mrjj

    I wonder if Derived class need

    It does, so why do you wonder? :)

    Read and abide by the Qt Code of Conduct

    M 1 Reply Last reply 18 Aug 2016, 12:39
    0
    • K kshegunov
      18 Aug 2016, 12:35

      @mrjj

      I wonder if Derived class need

      It does, so why do you wonder? :)

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 18 Aug 2016, 12:39 last edited by
      #4

      @kshegunov

      I often just wonder :)
      nah, i never used
      metaObject()->methodCount();

      so was not 100% sure.

      K 1 Reply Last reply 18 Aug 2016, 12:44
      0
      • M mrjj
        18 Aug 2016, 12:39

        @kshegunov

        I often just wonder :)
        nah, i never used
        metaObject()->methodCount();

        so was not 100% sure.

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 18 Aug 2016, 12:44 last edited by
        #5

        @mrjj
        QObject::metaObject() returns the QMetaObject associated with the class (it's a static member, and the method provides virtualization over the class tree). If you don't have the Q_OBJECT macro, there's no staticMetaObject member for the class, and consequently there's no metaObject() override, thus you get the parent's staticMetaObject (or the last class that had the macro). Like in this case, there can't be reflection for the derived type, because the needed meta information isn't generated (i.e. the missing macro); the best you get is the information for the parent class.

        Although, a need for this kind of introspection is rather rare ...

        Read and abide by the Qt Code of Conduct

        M 1 Reply Last reply 18 Aug 2016, 12:47
        2
        • K kshegunov
          18 Aug 2016, 12:44

          @mrjj
          QObject::metaObject() returns the QMetaObject associated with the class (it's a static member, and the method provides virtualization over the class tree). If you don't have the Q_OBJECT macro, there's no staticMetaObject member for the class, and consequently there's no metaObject() override, thus you get the parent's staticMetaObject (or the last class that had the macro). Like in this case, there can't be reflection for the derived type, because the needed meta information isn't generated (i.e. the missing macro); the best you get is the information for the parent class.

          Although, a need for this kind of introspection is rather rare ...

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 18 Aug 2016, 12:47 last edited by
          #6

          @kshegunov
          ahh, i never really examined the macro.
          So thats how it works.
          Its very cool.
          I wish it was a pure c++ feature :)

          K 1 Reply Last reply 18 Aug 2016, 12:53
          0
          • M mrjj
            18 Aug 2016, 12:47

            @kshegunov
            ahh, i never really examined the macro.
            So thats how it works.
            Its very cool.
            I wish it was a pure c++ feature :)

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 18 Aug 2016, 12:53 last edited by kshegunov
            #7

            @mrjj
            Actually it is, somewhat. See here. The moc is needed to generate the meta information about the class, and the methods implementations, but for the static members or virtual functions declarations we have the good ol' preprocessor. ;)

            Read and abide by the Qt Code of Conduct

            M 1 Reply Last reply 18 Aug 2016, 13:00
            0
            • X Offline
              X Offline
              XavierLL
              wrote on 18 Aug 2016, 13:00 last edited by
              #8

              Resolved!

              I tried with the Q_OBJECT before but didn't rerun qmake... my fault :) and the errors I was obtaining made me think that maybe I could only use Q_OBJECT in classes directly derived from QObject, didn't think deeply about it.

              I am just experimenting with all the metadata classes for a property based system UI.

              Thanks a lot!

              Xllr

              1 Reply Last reply
              1
              • K kshegunov
                18 Aug 2016, 12:53

                @mrjj
                Actually it is, somewhat. See here. The moc is needed to generate the meta information about the class, and the methods implementations, but for the static members or virtual functions declarations we have the good ol' preprocessor. ;)

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 18 Aug 2016, 13:00 last edited by
                #9

                @kshegunov
                oh. pretty neat.
                I just wish it could list
                members variables + type too :)

                I never used the Qt metasystem. (directly)
                Can moc be used to extract (plain) variables or does it has to be properties?

                K 1 Reply Last reply 18 Aug 2016, 13:04
                0
                • X Offline
                  X Offline
                  XavierLL
                  wrote on 18 Aug 2016, 13:03 last edited by
                  #10

                  Not sure, because I have been working with this for 2 days, but I think they must be properties.

                  1 Reply Last reply
                  0
                  • M mrjj
                    18 Aug 2016, 13:00

                    @kshegunov
                    oh. pretty neat.
                    I just wish it could list
                    members variables + type too :)

                    I never used the Qt metasystem. (directly)
                    Can moc be used to extract (plain) variables or does it has to be properties?

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 18 Aug 2016, 13:04 last edited by
                    #11

                    @mrjj said:

                    I just wish it could list
                    members variables + type too

                    Well, this wouldn't be very helpful, as you already know what member variables you have (and some of them may be private), or as in the usual case you only have a PIMPL pointer. You can however get the declared properties (such as declared with the Q_PROPERTY macro) and this is used extensively, e.g. in QML.

                    Read and abide by the Qt Code of Conduct

                    M 1 Reply Last reply 18 Aug 2016, 13:09
                    1
                    • K kshegunov
                      18 Aug 2016, 13:04

                      @mrjj said:

                      I just wish it could list
                      members variables + type too

                      Well, this wouldn't be very helpful, as you already know what member variables you have (and some of them may be private), or as in the usual case you only have a PIMPL pointer. You can however get the declared properties (such as declared with the Q_PROPERTY macro) and this is used extensively, e.g. in QML.

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 18 Aug 2016, 13:09 last edited by
                      #12

                      @kshegunov
                      well it would be extremely helpful for creating boilerplate code for serialization and
                      trace systems & module tests and all kind of code gen.
                      Without adding ANYTHING to the source code as all serialization
                      frameworks i have seen does.

                      Maybe in c++32 :)

                      K 1 Reply Last reply 18 Aug 2016, 13:15
                      0
                      • M mrjj
                        18 Aug 2016, 13:09

                        @kshegunov
                        well it would be extremely helpful for creating boilerplate code for serialization and
                        trace systems & module tests and all kind of code gen.
                        Without adding ANYTHING to the source code as all serialization
                        frameworks i have seen does.

                        Maybe in c++32 :)

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 18 Aug 2016, 13:15 last edited by
                        #13

                        @mrjj said:

                        well it would be extremely helpful for creating boilerplate code for serialization

                        You have QDataStream for that. My suspicion is that moc's source will just explode if you start adding more and more parsing features. It already does a lot, e.g. RTTI without compiler RTTI and of course the signal-slot mechanism. If you need to wrap some boilerplate code you can always use a combination of the preprocessor with virtualization (similarly to what Qt does). I, personally, use a virtual stream operator for such things:

                        class MyClass
                        {
                            friend QDataStream & operator << (QDataStream &, const MyClass &);
                            friend QDataStream & operator >> (QDataStream &, MyClass &);
                        
                        protected:
                            virtual bool serialize(QDataStream &) = 0;
                            virtual bool deserialize(QDataStream &) = 0;
                        }
                        
                        inline QDataStream & operator << (QDataStream & out, const MyClass & obj)
                        {
                            obj.serialize(out);
                            return out;
                        }
                        
                        inline QDataStream & operator >> (QDataStream & in, MyClass & obj)
                        {
                            obj.deserialize(in);
                            return in;
                        }
                        

                        Read and abide by the Qt Code of Conduct

                        M 1 Reply Last reply 18 Aug 2016, 13:23
                        2
                        • K kshegunov
                          18 Aug 2016, 13:15

                          @mrjj said:

                          well it would be extremely helpful for creating boilerplate code for serialization

                          You have QDataStream for that. My suspicion is that moc's source will just explode if you start adding more and more parsing features. It already does a lot, e.g. RTTI without compiler RTTI and of course the signal-slot mechanism. If you need to wrap some boilerplate code you can always use a combination of the preprocessor with virtualization (similarly to what Qt does). I, personally, use a virtual stream operator for such things:

                          class MyClass
                          {
                              friend QDataStream & operator << (QDataStream &, const MyClass &);
                              friend QDataStream & operator >> (QDataStream &, MyClass &);
                          
                          protected:
                              virtual bool serialize(QDataStream &) = 0;
                              virtual bool deserialize(QDataStream &) = 0;
                          }
                          
                          inline QDataStream & operator << (QDataStream & out, const MyClass & obj)
                          {
                              obj.serialize(out);
                              return out;
                          }
                          
                          inline QDataStream & operator >> (QDataStream & in, MyClass & obj)
                          {
                              obj.deserialize(in);
                              return in;
                          }
                          
                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 18 Aug 2016, 13:23 last edited by
                          #14

                          @kshegunov
                          Well I guess it then again boils down to
                          out << var1 << var2 << var3 pr class which is what
                          i would like NOT to have to ever write. :)

                          So if could

                          for ( all member vars : curvar)
                          out << curvar;

                          make me very happy.

                          It seems Qt properties would allow such thing ? :)

                          K 1 Reply Last reply 18 Aug 2016, 13:30
                          0
                          • M mrjj
                            18 Aug 2016, 13:23

                            @kshegunov
                            Well I guess it then again boils down to
                            out << var1 << var2 << var3 pr class which is what
                            i would like NOT to have to ever write. :)

                            So if could

                            for ( all member vars : curvar)
                            out << curvar;

                            make me very happy.

                            It seems Qt properties would allow such thing ? :)

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 18 Aug 2016, 13:30 last edited by kshegunov
                            #15

                            @mrjj

                            It seems Qt properties would allow such thing ? :)

                            Yes, you can list them, but then again you have to declare them with Q_PROPERTY, and also you might want to save internal data that's not exposed through a property ... which could pose a significant problem.

                            I prefer the mentioned method, because I can delegate to the parent. Consider the following example:

                            class MyClassImpl : public MyClass
                            {
                                // ...
                            
                            protected:
                                bool serialize(QDataStream & out) override
                                {
                                    out << x;
                                    return true;
                                }
                            
                                bool deserialize(QDataStream & in) override
                                {
                                    in >> x;
                                    return true;
                                }
                            
                            private:
                                int x;
                            }
                            
                            class MyDerivedClassImpl : public MyClassImpl
                            {
                                // ...
                            
                            protected:
                                bool serialize(QDataStream & out) override
                                {
                                    MyClassImpl::serialize(out);
                                    out << y << str;
                                    return true;
                                }
                            
                                bool deserialize(QDataStream & in) override
                                {
                                    MyClassImpl::deserialize(in);
                                    out >> y >> str;
                                    return true;
                                }
                            
                            private:
                                double y;
                                QString str;
                            }
                            

                            Read and abide by the Qt Code of Conduct

                            M 1 Reply Last reply 18 Aug 2016, 13:49
                            0
                            • K kshegunov
                              18 Aug 2016, 13:30

                              @mrjj

                              It seems Qt properties would allow such thing ? :)

                              Yes, you can list them, but then again you have to declare them with Q_PROPERTY, and also you might want to save internal data that's not exposed through a property ... which could pose a significant problem.

                              I prefer the mentioned method, because I can delegate to the parent. Consider the following example:

                              class MyClassImpl : public MyClass
                              {
                                  // ...
                              
                              protected:
                                  bool serialize(QDataStream & out) override
                                  {
                                      out << x;
                                      return true;
                                  }
                              
                                  bool deserialize(QDataStream & in) override
                                  {
                                      in >> x;
                                      return true;
                                  }
                              
                              private:
                                  int x;
                              }
                              
                              class MyDerivedClassImpl : public MyClassImpl
                              {
                                  // ...
                              
                              protected:
                                  bool serialize(QDataStream & out) override
                                  {
                                      MyClassImpl::serialize(out);
                                      out << y << str;
                                      return true;
                                  }
                              
                                  bool deserialize(QDataStream & in) override
                                  {
                                      MyClassImpl::deserialize(in);
                                      out >> y >> str;
                                      return true;
                                  }
                              
                              private:
                                  double y;
                                  QString str;
                              }
                              
                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 18 Aug 2016, 13:49 last edited by
                              #16

                              @kshegunov
                              Yeah, 50% is not Qt enabled so would be an issue.
                              Im just daydreaming :)

                              Hmm, that is actually neat.

                              1 Reply Last reply
                              0

                              12/16

                              18 Aug 2016, 13:09

                              • Login

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