[Development] QProperty and library coding guide

Volker Hilsheimer volker.hilsheimer at qt.io
Thu Jul 16 17:42:06 CEST 2020

> On 16 Jul 2020, at 14:03, Stottlemyer, Brett (B.S.) <bstottle at ford.com> wrote:
> Hi Ulf,
> > QProperty is the way to enable QML-style bindings in C++. It gives you a 
> > powerful way of expressing relations between properties in a succinct 
> > way. You can assign a binding functor directly to a QProperty. Any 
> > QProperties the functor accesses are automatically recorded. Whenever 
> > one of the recorded QProperties changes, the functor is (eventually) 
> > re-evaluated and the original property is updated. You don't have to 
> > remember to connect any signals. This is a big step forward as 
> > connecting all relevant signals to all relevant slots and manually 
> > re-evaluating everything that depends on any changes adds a lot of error 
> > prone boiler plate to many projects.
> This is a first-heard for me.  Have you thought about how this would work with the Remote Objects module?  I'm worried it would not work well at all...
> Is there a helloworld example available so people can get a better handle on what this is doing and how it works?
> Thanks,
> Brett

Hey Brett,

The public interface of the QObject subclasses won’t change. You can still get the list of properties from the metaobject, and get and set each property by using a name (as C-string/byte-array)/QVariant. moc will generate the relevant boilerplate code, and there’ll be the same getter and setter methods as before, albeit declared through the macro.

So QObject serialization should work as before.

There are some hairy cases where properties are not orthogonal, but depend on each other in interesting ways. Those were hairy before, and making sure that we have tests to make sure that we maintain behavior compatibility is part of the migration exercise. And there will be cases where the benefit of bindings is minor enough not to bother with changing anything.

So, enabling binding-syntax from C++ is an additional feature, and you don’t need to use it. I assume you won’t be able to benefit from it in the Remote Objects framework. Perhaps at some later point we could allow someting that enables bindings using property names, such as:

object->bindProperty(“foo”, otherObject, “bar”); // object::foo = otherObject::bar

That would require more moc boilerplate code, I suppose, but it’s probably not that useful anyway.


More information about the Development mailing list