[Development] QProperty and library coding guide

André Somers andre at familiesomers.nl
Wed Jul 22 13:52:58 CEST 2020


On 16-07-2020 17:40, Volker Hilsheimer wrote:
>
> We need “text” to be a public member of QAction, otherwise we can’t do
>
> action->text();
>
>
> That ‘text” member is a struct with a bunch of operators overloaded, so that we can do either
>
> qDebug() << action->text();
> qDebug() << action->text;
>
> and
>
> action->setText(“foo”); // no binding
> action->text = document->title; // still no binding
> action->text = Qt::makePropertyBinding(document->title); // yay binding!
>
Thank you for this example of how these new properties are going to look 
from a user code perspective. I have some questions left if you don't mind?

First, say I have another QProperty that has a binding to this 
action->text property. I assume that doing `action->setText("foo")` or 
`action->text = document->title()` will both result in this binding 
being triggered, and thus, triggering side-effects? I would expect such 
in the first case, but not so much in the second as this _looks_ like an 
assignment to a member variable.

Second, say that I initialized action->text like you do in your last 
example, creating a binding. And then I assign to it using, say, 
`action->text = document->title();` Does that work? Do we overwrite the 
binding? Is it possible to prevent that override? This was a problem in 
QML as well where one could accidentally overwrite bindings. I assume 
there is a way to mimic adding `readonly` like you can in QML?

Third, you show that `qDebug() << action.text()` and `qDebug() << 
action.text` is the same. Is that really all that desirable? Somebody 
already pointed out this issues with doing an `auto actionText = 
action.text();` elsewhere in this thread, and I am wondering if you'd 
not want to output some debug info on the property itself, in stead of 
just the value?


Don't get me wrong: I think the syntax where you're basically able to 
use the property as if it was a member variable, basically coming back 
full circle. But I also think it may be confusing for everyone expecting 
an assignment to something that looks like a member to, well, behave 
like that. Is there a reason why this setup was chosen over a more 
conventional but admittedly less cool:

action->text->set("foo");
action->text->set(document->title->get());

action->text->bind(document->title);

qDebug() << action->text.get();


Cheers,

André





More information about the Development mailing list