A "wholesome" QProperty
-
It's a common practice, define private data member, define public (or protected) data accessor (set/get methods).
I've been looking at QProperty to help me reduce lines of code, but found out it requires me to write the set/get methods. Is there a more "wholesome" Qt Macro that would also provide the content of the simple "get/set" methods?
I know, I can easily write one myself, just wanted to know if Qt already has it.
-
I think "this discussion":http://developer.qt.nokia.com/forums/viewthread/4382/ may interest you.
-
an easy way is define your own macro if you always have the same thing to do :
@#define commonMember(type,x)
protected:
type m_ ## x
public:
type get ## x () { return m_ ## x;}
void set ## x (type a){ m_## x = a;}
@From my point of view however, it tends to ugliness : I prefer to write my trivial getters on demand, if required (less code, less bug, less test)
_Macro not compiled, not tested, so not working, it's just to give the spirit of it ;-) _
-
Ok,
I tried it out. This works (is compilable):
@
#define commonMember(type,x)
protected:
type m_ ## x;
public:
type get ## x () { return m_ ## x;}
void set ## x (type a){ m_ ## x = a;}class MainWindow : public QMainWindow
{
Q_OBJECT
Q_PROPERTY(bool test READ gettest WRITE settest)commonMember(bool, test)
public:
@
-
Is it still impossible to generate Q_PROPERTY from a macro?
-
"Qt 5.1 introduces":http://qt-project.org/wiki/New-Features-in-Qt-5.1 a new keyword for Q_PROPERTY:
@New keyword in Q_PROPERTY: MEMBER let you bind a property to a class member without requiring to have a getter or a setter.@