Make adding new Q_PROPERTY easy for the developer
-
I have a lot of classes which a lot of properties shared with QML. To define such a property there is lot of source code needed like this:
@private:
Q_PROPERTY(bool button1 READ getButton1 WRITE setButton1 NOTIFY button1Changed)
public:
bool getButton1() const { return button1; }
signals:
void button1Changed();
public slots:
void setButton1 (bool localParameter);
private:
bool button1;@The idea is to simplify this process in any way to make it easy for new developers to add a property and minimize possible naming mistakes. I started to define a macro which creates the code and is called with:
@MYDATAPROPERTY(bool, button1, Button1)@
This works not and I have learned from the forum that the moc compiler works before the c-preprocessor works and in this case the solution is not usable.
Are there other methods to simplify the process of code generation?
-
In Qt 5.1, you can use the new keyword: "MEMBER". I have not tested it myself, but it should make your life easier. See "this link":http://qt-project.org/wiki/New-Features-in-Qt-5.1.
-
Thanks for the quick response. The MEMBER keyword is helpful to share members with QML only. The member is added the the Qt meta system but no getter/setter function is generated. But we need this functions to set/get values from the C++ side.
And it looks that the MEMBER keyword is not supported by QtCreator (2.8.1 based on Qt 5.1.1) -
It should still be possible to use it in C++ thorough MOC (QObject::property()), although the lack of setters and getters might be unwelcome.
If that is not enough, you can write your own code generator that will expand the macro into a set of getters and setters. Not an optimal solution, but possible. Another option would be to modify moc and push the code back to Qt Project, so that we can all benefit from it.