[Interest] Better Q_PROPERTY() ?

Jean-Michaël Celerier jeanmichael.celerier at gmail.com
Wed Dec 5 19:29:27 CET 2018


If it's Q_PROPERTY grievance time, can we add static, compile-time
reflection of the property's attributes ?

here's the macro I've quickly cobbled for myself, if it can be useful to
anyone else :
https://github.com/jcelerier/verdigris/blob/master/src/wobjectdefs.h#L918 ;
it has been extremely useful for me to generate a lot of UI stuff easily as
well as implementing a generic command pattern (that doesn't hide
everything behind a QVariant).

Best,
Jean-Michaël


-------
Jean-Michaël Celerier
http://www.jcelerier.name


On Wed, Dec 5, 2018 at 6:33 PM Vlad Stelmahovsky <vladstelmahovsky at gmail.com>
wrote:

> 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
> _______________________________________________
> Interest mailing list
> Interest at lists.qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20181205/d087d60e/attachment.html>


More information about the Interest mailing list