简体   繁体   English

包含Q_OBJECT的c ++宏

[英]c++ macro containing Q_OBJECT

I'm developping a c++/Qt program that will have several plugins. 我正在开发一个有几个插件的c ++ / Qt程序。 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): 我创建了一个宏(实际上是2,因为我不是c ++预处理器大师):

#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: 所以在我的代码中(定义了qwerty )我只需要添加:

MY_PLUGIN(qwerty)

which will expand (output of g++ -E ): 这将扩展( 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 : 当我编译我的项目时,我从moc得到错误:

Error: Class declaration lacks Q_OBJECT macro.

Do anyone have an idea? 有人有想法吗?

It turns out that as suggested by @Silicomancer you cannot as it is. 事实证明,正如@Silicomancer所建议的那样,你不能这样。

I figured out that the proble was the multi-line macro and it works indeed if the macro is one-line: 我发现问题是多行宏,如果宏是一行的话它确实有效:

#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;} };

To me it looks like moc goes through the file looking for Q_OBJECT but doesn't mess with macros 对我来说,看起来moc通过文件寻找Q_OBJECT但不会弄乱宏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM