[Development] RFC: RAII for property changes

Keith Gardner kreios4004 at gmail.com
Wed Apr 15 16:54:45 CEST 2015


On Wed, Apr 15, 2015 at 9:38 AM Marc Mutz <marc.mutz at kdab.com> wrote:

> Hi André,
>
> On Wednesday 15 April 2015 11:49:56 André Somers wrote:
> > void MyClass::setFoo(QString value)
> > {
> >    PropertyGuard guard(this, "foo");  //foo is the name of the Q_PROPERTY
> >    Q_UNUSED(guard);
> >
> >    m_foo = value;
> > }
>
> This is an interesting idea, though I don't think I have encountered the
> problems with which you motivate PropertyGuard.
>
> For use in a library, though, I fear the string-based mechanism is too
> inefficient. For use within QtWidgets, say, I'd suggest a mechanism that
> works
> on the member data directly.
>
> Thanks,
> Marc
>

I have actually run into the same situation and made a template class that
owns the variable.  Its constructor takes an initial value and a
std::function<void (const T&)> as a callback for when the value changes.
The callback can be a lambda or a std::bind to the expected signal.  I also
added overloads to allow for the templated class to behave just like the
contained type so that it can be swapped in easily.  I figured the Qt
project wouldn't like the submission of the class due to its template
nature and its use of std::function but i am willing to share it if anyone
is interested.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20150415/38901a24/attachment.html>


More information about the Development mailing list