ale*_*ail 5 c++ qt4 c-preprocessor
我正在设计一个QT4课程.到目前为止我的班级看起来像这样:
class GIHNode : public QObject, public QGraphicsItem
{
Q_OBJECT
Q_INTERFACES( QGraphicsItem )
[...]
public:
void setInteger(int);
int getInteger();
[..]
private:
int Integer; Q_PROPERTY(int Integer READ getInteger WRITE setInteger)
// Definition I'd like to replace
The setters and getters are implemented like this:
void GIHNode::setInteger(int x){Integer=x;}
int GIHNode::getInteger(){return Integer;}
Run Code Online (Sandbox Code Playgroud)
我想定义一个宏来完成所有这些工作.我试过这个来定义一个变量和里面的文字Q_PROPERTY:
#define ID(x) x
#define STR_HELPER(x,y) ID(x)y
#define STRGET(x) STR_HELPER(get,x)
#define STRSET(x) STR_HELPER(set,x)
#define EXPORTEDVAR(type,varname) type varname; Q_PROPERTY(type varname READ STRGET(varname) WRITE STRSET(varname))
Run Code Online (Sandbox Code Playgroud)
当我更换线路时:
int Integer; Q_PROPERTY(int Integer READ getInteger WRITE setInteger)
Run Code Online (Sandbox Code Playgroud)
同
EXPORTEDVAR(int,Integer)
Run Code Online (Sandbox Code Playgroud)
宏被正确预处理,并被替换为:
int Integer; Q_PROPERTY(int Integer READ getInteger WRITE setInteger)
Run Code Online (Sandbox Code Playgroud)
我用cpp手动检查了.代码编译,但我无法获取此类实例的属性.我正在使用此类实例的元对象(来自QT4 moc)来检索属性,但我找不到它.我想这与预处理器有关,但我不知道如何调查这个.