Skip to content

Instantly share code, notes, and snippets.

@Rolias
Last active October 22, 2025 06:06
Show Gist options
  • Save Rolias/48d453a0490d36090193 to your computer and use it in GitHub Desktop.
Save Rolias/48d453a0490d36090193 to your computer and use it in GitHub Desktop.

Revisions

  1. Rolias revised this gist Dec 4, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PropertyHelper.h
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@
    } \
    Q_SIGNAL void NAME ## Changed(TYPE value);\
    private: \
    TYPE a_ ## NAME{};
    TYPE a_ ## NAME;

    #define READONLY_PROPERTY(TYPE, NAME) \
    Q_PROPERTY(TYPE NAME READ NAME CONSTANT ) \
  2. Rolias revised this gist Nov 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PropertyHelper.h
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #pragma once
    #include <QObject>
    //See Gist Comment for description, usage, and warnings
    //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: \
  3. Rolias revised this gist Nov 5, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion PropertyHelper.h
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@
    #include <QObject>
    //See Gist Comment for description, usage, and warnings
    #define AUTO_PROPERTY(TYPE, NAME) \
    \
    Q_PROPERTY(TYPE NAME READ NAME WRITE NAME NOTIFY NAME ## Changed ) \
    public: \
    TYPE NAME() const { return a_ ## NAME ; } \
  4. Rolias revised this gist Nov 5, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions PropertyHelper.h
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@
    #define READONLY_PROPERTY(TYPE, NAME) \
    Q_PROPERTY(TYPE NAME READ NAME CONSTANT ) \
    public: \
    TYPE PROP_NAME() const { return a_ ## PROP_NAME ; } \
    TYPE NAME() const { return a_ ## NAME ; } \
    private: \
    void PROP_NAME(TYPE value) {a_ ## PROP_NAME = value; } \
    TYPE a_ ## PROP_NAME;
    void NAME(TYPE value) {a_ ## NAME = value; } \
    TYPE a_ ## NAME;
  5. Rolias revised this gist Nov 5, 2014. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions PropertyHelper.h
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,22 @@
    #pragma once
    #include <QObject>
    //See Gist Comment for description, usage, and warnings
    #define AUTO_PROPERTY(TYPE, PROP_NAME) \
    #define AUTO_PROPERTY(TYPE, NAME) \
    \
    Q_PROPERTY(TYPE PROP_NAME READ PROP_NAME WRITE PROP_NAME NOTIFY PROP_NAME ## Changed ) \
    Q_PROPERTY(TYPE NAME READ NAME WRITE NAME NOTIFY NAME ## Changed ) \
    public: \
    TYPE PROP_NAME() const { return a_ ## PROP_NAME ; } \
    void PROP_NAME(TYPE value) { \
    if (a_ ## PROP_NAME == value) return; \
    a_ ## PROP_NAME = value; \
    emit PROP_NAME ## Changed(value); \
    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 PROP_NAME ## Changed(TYPE value);\
    Q_SIGNAL void NAME ## Changed(TYPE value);\
    private: \
    TYPE a_ ## PROP_NAME{};
    TYPE a_ ## NAME{};

    #define READONLY_PROPERTY(TYPE, PROP_NAME) \
    Q_PROPERTY(TYPE PROP_NAME READ PROP_NAME CONSTANT ) \
    #define READONLY_PROPERTY(TYPE, NAME) \
    Q_PROPERTY(TYPE NAME READ NAME CONSTANT ) \
    public: \
    TYPE PROP_NAME() const { return a_ ## PROP_NAME ; } \
    private: \
  6. Rolias revised this gist Nov 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PropertyHelper.h
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #pragma once
    #include <QObject>
    //See Comment for description, usage, and warnings
    //See Gist Comment for description, usage, and warnings
    #define AUTO_PROPERTY(TYPE, PROP_NAME) \
    \
    Q_PROPERTY(TYPE PROP_NAME READ PROP_NAME WRITE PROP_NAME NOTIFY PROP_NAME ## Changed ) \
  7. Rolias revised this gist Nov 5, 2014. 1 changed file with 11 additions and 13 deletions.
    24 changes: 11 additions & 13 deletions PropertyHelper.h
    Original file line number Diff line number Diff line change
    @@ -5,22 +5,20 @@
    \
    Q_PROPERTY(TYPE PROP_NAME READ PROP_NAME WRITE PROP_NAME NOTIFY PROP_NAME ## Changed ) \
    public: \
    TYPE PROP_NAME() const { return a_ ## PROP_NAME ; } \
    void PROP_NAME(TYPE value) \
    { \
    if (a_ ## PROP_NAME == value) \
    return; \
    a_ ## PROP_NAME = value; \
    emit PROP_NAME ## Changed(value); \
    } \
    Q_SIGNAL void PROP_NAME ## Changed(TYPE value);\
    TYPE PROP_NAME() const { return a_ ## PROP_NAME ; } \
    void PROP_NAME(TYPE value) { \
    if (a_ ## PROP_NAME == value) return; \
    a_ ## PROP_NAME = value; \
    emit PROP_NAME ## Changed(value); \
    } \
    Q_SIGNAL void PROP_NAME ## Changed(TYPE value);\
    private: \
    TYPE a_ ## PROP_NAME{};
    TYPE a_ ## PROP_NAME{};

    #define READONLY_PROPERTY(TYPE, PROP_NAME) \
    Q_PROPERTY(TYPE PROP_NAME READ PROP_NAME CONSTANT ) \
    public: \
    TYPE PROP_NAME() const { return a_ ## PROP_NAME ; } \
    TYPE PROP_NAME() const { return a_ ## PROP_NAME ; } \
    private: \
    void PROP_NAME(TYPE value) {a_ ## PROP_NAME = value; } \
    TYPE a_ ## PROP_NAME;
    void PROP_NAME(TYPE value) {a_ ## PROP_NAME = value; } \
    TYPE a_ ## PROP_NAME;
  8. Rolias revised this gist Nov 4, 2014. 1 changed file with 1 addition and 52 deletions.
    53 changes: 1 addition & 52 deletions PropertyHelper.h
    Original file line number Diff line number Diff line change
    @@ -1,57 +1,6 @@
    #pragma once
    #include <QObject>
    //DESCRIPTION:
    //These macros are intended to reduce the amount of boilerplate code the
    //user has to write when using the Qt Q_PROPERTY feature. The idea was to
    //provide something similar to C#'s auto property feature.
    //
    //AUTO_PROPERTY(TYPE, NAME) will create a standard
    //Q_PROPERTY (TYPE, NAME READ NAME WRITE Name NOTIFY NAMEChanged) of the
    //passed type and name. In addition, it will create a standard getter named NAME,
    //setter named Name and signal named NAMEChanged(TYPE value)
    //It will also create the member data variable.
    //Note that the macro doesn't use the standard set prefix followed by a capitalized
    //first letter for the setter. I wasn't sure how to do this in a Macro and I prefer
    //having the same syntax for the getter and setter.
    //The member var is named with an a_ prefix (for auto) because it is intended
    //to remind the user to *NOT* use the member variable name directly. The user
    //should use the property name instead. If you don't like the a_ prefix simply change
    //the macro.
    //
    //READONLY_PROPERTY(TYPE, NAME) will create a standard
    //Q_PROPERTY( TYPE NAME READ NAME CONST)
    //It will create the getter and the member variable name. In this macro
    //the member variable again uses a_ but the user is expected to initialize this
    //variable directly. The variable shouldn't be upated.
    // All reads of the variable should be done through the property
    //
    //NOTE: Use these macros in the "private" section of your header only.
    //They use both public and private keywords and leave the header in the
    //private section. Putting these macros direcly under the Q_OBJECT macro
    //at the top of the class is acceptable.
    //
    //EXAMPLE USAGES:
    // AUTO_PROPERTY(QString, myProperty)
    // READONLY_PROPERTY(double, myValueProp)
    //
    //WARNING: Note that these macros are only intended to be used as shown.
    //The parameters are NOT intended to be expressions but simply the type and
    //name. The parameters aren't parenthesized in the body of the macro and the entire
    //macro is not parenthesized. Use responsibly.
    //
    // NOTE ON INITIALIZERS
    //If you uses these macros at the top of your class then the member variables
    //they create will be the first ones in your class and therefore to make tools
    //like lint happy you should add these variables to your initializer list in the
    //order they are declared and before any variables that come after these macros.
    //If you want to fully initialize variables in the order they are declared then
    //even AUTO variables will need to be accessed directly. There is no harm in doing
    //this. In general I was trying to adhere to the philosophy of the member variables
    //being hidden and only the property being used. If you have an initialization
    //order dependency on auto properties it might be a code smell and that variable
    //might be better off not being an auto property.


    //See Comment for description, usage, and warnings
    #define AUTO_PROPERTY(TYPE, PROP_NAME) \
    \
    Q_PROPERTY(TYPE PROP_NAME READ PROP_NAME WRITE PROP_NAME NOTIFY PROP_NAME ## Changed ) \
  9. Rolias revised this gist Nov 4, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions PropertyHelper.h
    Original file line number Diff line number Diff line change
    @@ -10,11 +10,11 @@
    //passed type and name. In addition, it will create a standard getter named NAME,
    //setter named Name and signal named NAMEChanged(TYPE value)
    //It will also create the member data variable.
    //The macro could easily be changed to use setName for the setter but I prefer
    //this syntax. If you want a set prefix add set_ ## NAME in both the Q_PROPERTY
    //and function signature.
    //The member var is named with an a_ prefix (for auto) because it is intened
    //to remind the user to NOT use the member variable name directly. The user
    //Note that the macro doesn't use the standard set prefix followed by a capitalized
    //first letter for the setter. I wasn't sure how to do this in a Macro and I prefer
    //having the same syntax for the getter and setter.
    //The member var is named with an a_ prefix (for auto) because it is intended
    //to remind the user to *NOT* use the member variable name directly. The user
    //should use the property name instead. If you don't like the a_ prefix simply change
    //the macro.
    //
  10. Rolias created this gist Nov 4, 2014.
    77 changes: 77 additions & 0 deletions PropertyHelper.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    #pragma once
    #include <QObject>
    //DESCRIPTION:
    //These macros are intended to reduce the amount of boilerplate code the
    //user has to write when using the Qt Q_PROPERTY feature. The idea was to
    //provide something similar to C#'s auto property feature.
    //
    //AUTO_PROPERTY(TYPE, NAME) will create a standard
    //Q_PROPERTY (TYPE, NAME READ NAME WRITE Name NOTIFY NAMEChanged) of the
    //passed type and name. In addition, it will create a standard getter named NAME,
    //setter named Name and signal named NAMEChanged(TYPE value)
    //It will also create the member data variable.
    //The macro could easily be changed to use setName for the setter but I prefer
    //this syntax. If you want a set prefix add set_ ## NAME in both the Q_PROPERTY
    //and function signature.
    //The member var is named with an a_ prefix (for auto) because it is intened
    //to remind the user to NOT use the member variable name directly. The user
    //should use the property name instead. If you don't like the a_ prefix simply change
    //the macro.
    //
    //READONLY_PROPERTY(TYPE, NAME) will create a standard
    //Q_PROPERTY( TYPE NAME READ NAME CONST)
    //It will create the getter and the member variable name. In this macro
    //the member variable again uses a_ but the user is expected to initialize this
    //variable directly. The variable shouldn't be upated.
    // All reads of the variable should be done through the property
    //
    //NOTE: Use these macros in the "private" section of your header only.
    //They use both public and private keywords and leave the header in the
    //private section. Putting these macros direcly under the Q_OBJECT macro
    //at the top of the class is acceptable.
    //
    //EXAMPLE USAGES:
    // AUTO_PROPERTY(QString, myProperty)
    // READONLY_PROPERTY(double, myValueProp)
    //
    //WARNING: Note that these macros are only intended to be used as shown.
    //The parameters are NOT intended to be expressions but simply the type and
    //name. The parameters aren't parenthesized in the body of the macro and the entire
    //macro is not parenthesized. Use responsibly.
    //
    // NOTE ON INITIALIZERS
    //If you uses these macros at the top of your class then the member variables
    //they create will be the first ones in your class and therefore to make tools
    //like lint happy you should add these variables to your initializer list in the
    //order they are declared and before any variables that come after these macros.
    //If you want to fully initialize variables in the order they are declared then
    //even AUTO variables will need to be accessed directly. There is no harm in doing
    //this. In general I was trying to adhere to the philosophy of the member variables
    //being hidden and only the property being used. If you have an initialization
    //order dependency on auto properties it might be a code smell and that variable
    //might be better off not being an auto property.


    #define AUTO_PROPERTY(TYPE, PROP_NAME) \
    \
    Q_PROPERTY(TYPE PROP_NAME READ PROP_NAME WRITE PROP_NAME NOTIFY PROP_NAME ## Changed ) \
    public: \
    TYPE PROP_NAME() const { return a_ ## PROP_NAME ; } \
    void PROP_NAME(TYPE value) \
    { \
    if (a_ ## PROP_NAME == value) \
    return; \
    a_ ## PROP_NAME = value; \
    emit PROP_NAME ## Changed(value); \
    } \
    Q_SIGNAL void PROP_NAME ## Changed(TYPE value);\
    private: \
    TYPE a_ ## PROP_NAME{};

    #define READONLY_PROPERTY(TYPE, PROP_NAME) \
    Q_PROPERTY(TYPE PROP_NAME READ PROP_NAME CONSTANT ) \
    public: \
    TYPE PROP_NAME() const { return a_ ## PROP_NAME ; } \
    private: \
    void PROP_NAME(TYPE value) {a_ ## PROP_NAME = value; } \
    TYPE a_ ## PROP_NAME;