Last active
September 27, 2023 21:58
-
-
Save Rolias/48d453a0490d36090193 to your computer and use it in GitHub Desktop.
Qt Auto Property Macros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.