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. C++ macro containing Q_OBJECT: moc error
Forum Updated to NodeBB v4.3 + New Features

C++ macro containing Q_OBJECT: moc error

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 5.4k 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.
  • B Offline
    B Offline
    bibi
    wrote on last edited by A Former User
    #1

    I'm developping a c++/Qt program that will have several plugins.
    For each class I have to define a plugin-interface that looks like this:

    //my class
    class qwerty;
    
    //my interface
    class qwertyPlug : public QObject, myPlug { 
    Q_OBJECT 
    Q_INTERFACES(nPanPlug) 
    Q_PLUGIN_METADATA(IID "org.myapp.plug") 
    public: 
      qwertyPlug() {qRegisterMetaType<qwerty *>("qwerty""*");} 
      QByteArray name() {return "qwerty";} 
    };
    

    I created a macro (actually 2 since I'm not a c++ preprocessor guru):

    #define MY_PLUGIN_BASE(__c_name,__appendix)                                 \
      class __c_name##__appendix : public QObject, myPlug {                     \
      Q_OBJECT                                                                  \
      Q_INTERFACES(nPanPlug)                                                    \
      Q_PLUGIN_METADATA(IID "org.myapp.plug")                                   \
      public:                                                                   \
          __c_name##__appendix() {qRegisterMetaType<__c_name *>(#__c_name"*");} \
          QByteArray name() {return #__c_name;}                                 \
      };
    
    #define MY_PLUGIN(__c_name) MY_PLUGIN_BASE(__c_name,Plug)
    

    so that In my code (where qwerty is defined) I just have to add:

    MY_PLUGIN(qwerty)
    

    which will expand (output of g++ -E):

    class qwertyPlug : public QObject, myPlug { Q_OBJECT Q_INTERFACES(nPanPlug) Q_PLUGIN_METADATA(IID "org.myapp.plug") public: qwertyPlug() {qRegisterMetaType<qwerty *>("qwerty""*");} QByteArray name() {return "qwerty";} };
    

    and it looks ok (sorry for the readability but I dont know how to add newlines..) and works if copy/paste the above line in my code but...

    When I compile my project I get errors from the moc:

    Error: Class declaration lacks Q_OBJECT macro.
    

    Do anyone have an idea?

    p.s. here is my SO question
    http://stackoverflow.com/questions/41056941/c-macro-containing-q-object

    raven-worxR 1 Reply Last reply
    0
    • B bibi

      I'm developping a c++/Qt program that will have several plugins.
      For each class I have to define a plugin-interface that looks like this:

      //my class
      class qwerty;
      
      //my interface
      class qwertyPlug : public QObject, myPlug { 
      Q_OBJECT 
      Q_INTERFACES(nPanPlug) 
      Q_PLUGIN_METADATA(IID "org.myapp.plug") 
      public: 
        qwertyPlug() {qRegisterMetaType<qwerty *>("qwerty""*");} 
        QByteArray name() {return "qwerty";} 
      };
      

      I created a macro (actually 2 since I'm not a c++ preprocessor guru):

      #define MY_PLUGIN_BASE(__c_name,__appendix)                                 \
        class __c_name##__appendix : public QObject, myPlug {                     \
        Q_OBJECT                                                                  \
        Q_INTERFACES(nPanPlug)                                                    \
        Q_PLUGIN_METADATA(IID "org.myapp.plug")                                   \
        public:                                                                   \
            __c_name##__appendix() {qRegisterMetaType<__c_name *>(#__c_name"*");} \
            QByteArray name() {return #__c_name;}                                 \
        };
      
      #define MY_PLUGIN(__c_name) MY_PLUGIN_BASE(__c_name,Plug)
      

      so that In my code (where qwerty is defined) I just have to add:

      MY_PLUGIN(qwerty)
      

      which will expand (output of g++ -E):

      class qwertyPlug : public QObject, myPlug { Q_OBJECT Q_INTERFACES(nPanPlug) Q_PLUGIN_METADATA(IID "org.myapp.plug") public: qwertyPlug() {qRegisterMetaType<qwerty *>("qwerty""*");} QByteArray name() {return "qwerty";} };
      

      and it looks ok (sorry for the readability but I dont know how to add newlines..) and works if copy/paste the above line in my code but...

      When I compile my project I get errors from the moc:

      Error: Class declaration lacks Q_OBJECT macro.
      

      Do anyone have an idea?

      p.s. here is my SO question
      http://stackoverflow.com/questions/41056941/c-macro-containing-q-object

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @bibi
      are you using Qt4?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        http://stackoverflow.com/questions/37603455/strange-preprocessor-behaviour-when-mocing-a-header-file-in-qt

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1

        • Login

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