Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to generate setters and getters, and define Q_PROPERTY() with the C++ preprocessor
    primarykey
    data
    text
    <p>I'm designing a QT4 class. So far my class looks like this:</p> <pre><code>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;} </code></pre> <p>I'd like to define a macro that does all this work for me. I have tried this for the definition of a variable and the text inside <code>Q_PROPERTY</code>:</p> <pre><code>#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)) </code></pre> <p>When I replace the line:</p> <pre><code>int Integer; Q_PROPERTY(int Integer READ getInteger WRITE setInteger) </code></pre> <p>with </p> <pre><code>EXPORTEDVAR(int,Integer) </code></pre> <p>the macro is correctly pre-processed, and it's replaced with:</p> <pre><code>int Integer; Q_PROPERTY(int Integer READ getInteger WRITE setInteger) </code></pre> <p>I have checked manually with cpp. The code compiles, but I can't get the property of an instance of this class. I'm using the metaobject (from QT4 moc) of an instance of this class to retrieve the properties, but I can't find it. I suppose this has something to do with the pre-processor, but I don't know how to investigate this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload