[Interest] Better Q_PROPERTY() ?

Vlad Stelmahovsky vladstelmahovsky at gmail.com
Wed Dec 5 18:14:09 CET 2018


use QtCreator, so you can autogenerate most of the code there

On 12/5/18 5:41 PM, Jason H wrote:
> I've been doing a lot of Q_PROPERTY stuff again, and I waste too much time writing boiler plate code. Last time, I was reminded of the MEMBER modifier, which is what I thought I wanted. But after having worked with it some more, it's not what I want.
>
> Given:
> Q_PROPERTY (qreal scale READ scale WRITE setScale NOTIFY scaleChanged)  // What I end up writing
> Q_PROPERTY (qreal scale MEMBER _scale NOTIFY) // What I want to write
>
> I want the member form to also declare/implement:
> public:
> qreal scale() { return _scale; }
> void setScale(qreal scale) { if (scale != scale) { _scale = scale; emit scaleChanged(scale); } }
> signal:
> void scaleChanged(qreal scale);
>
> Where T=type, N=name M=member
> public:
> T N() { return M; }
> void setN(T N) { if (M != N) { M = N; emit NChanged(N); } }
> signal:
> void NChanged(T N);
>
> I'm trying to think of how to do this, and this seems doable:
>
> class X {
>
> ...
> INCLUDE_AUTOMOC_DECLARATIONS
> };
>
> Where
> INCLUDE_AUTOMOC_DECLARATIONS expands to:
> #include "filename_X_automoc.h"
>
> Where MOC has written the declarations. Similarly, there can be one for implementations as well.
>
> Ideally though, all I should need to write:
> Q_PROPERTY (qreal scale NOTIFY)
>
> Epanding to:
> private:
> T _N;
> public:
> T N() { return _N; }
> void setN(T N) { if (_N != N) { M = N; emit NChanged(N); } }
> signal:
> void NChanged(T N);
>
> I know this might sound trivial but if I'm making 5 classes each with 10 properties, that's 1500 lines of boilerplate code that I'm writing (with code style applied).
>
> Is there any way to get closer to my ideal?
> _______________________________________________
> Interest mailing list
> Interest at lists.qt-project.org
> https://lists.qt-project.org/listinfo/interest



More information about the Interest mailing list