-
-
Save Rolias/48d453a0490d36090193 to your computer and use it in GitHub Desktop.
#pragma once | |
#include <QObject> | |
//See Gist Comment for description, usage, warnings and license information | |
#define AUTO_PROPERTY(TYPE, NAME) \ | |
Q_PROPERTY(TYPE NAME READ NAME WRITE NAME NOTIFY NAME ## Changed ) \ | |
public: \ | |
TYPE NAME() const { return a_ ## NAME ; } \ | |
void NAME(TYPE value) { \ | |
if (a_ ## NAME == value) return; \ | |
a_ ## NAME = value; \ | |
emit NAME ## Changed(value); \ | |
} \ | |
Q_SIGNAL void NAME ## Changed(TYPE value);\ | |
private: \ | |
TYPE a_ ## NAME; | |
#define READONLY_PROPERTY(TYPE, NAME) \ | |
Q_PROPERTY(TYPE NAME READ NAME CONSTANT ) \ | |
public: \ | |
TYPE NAME() const { return a_ ## NAME ; } \ | |
private: \ | |
void NAME(TYPE value) {a_ ## NAME = value; } \ | |
TYPE a_ ## NAME; |
This is a great idea, but unfortunately, setProperty does not work with this. The reason is that moc does not create the property because it doesn't understand the AUTO_PROPERTY macro. Do you know a solution for this?
Update: Aha! Moc does expand macros, since Qt5. I only have to take care that moc has the same include path as the C++ compiler, or otherwise make sure that moc actually finds PropertyHelper.h.
Hi, thanks for your great working
I have some ideas for updating the file.
-
Use reference in function
WRITE
inAUTO_PROPERTY
void NAME(const TYPE& value)
-
Remove function
WRITE
inREADONLY_PROPERTY
void NAME(TYPE value) {a_ ## NAME = value; }
-
Add define
READ_PROPERTY
with the Q_SIGNAL for qml can get the notify when property changed in C++.
QML can not change the property.
You can see details in https://gist.github.com/tackelua/bcae0e346fc98184ba7a044315fd0a4c/revisions#diff-b6a69a742adff907546e80f3703e301a
12/4/2014 Fixed wayward pair of {} after private type declaration.