[Interest] Is it safe to construct QBindable from QProperty* via QUntypedBindable?

Axel Spoerl axel.spoerl at qt.io
Fri May 24 15:01:32 CEST 2024


Seas Robert,

If memory serves well, there was a recent post about the same question on the Qt Forum.
Haven't got around to answer, because I wanted to read some code upfront.

The given implementation of getBindable() has no return type, but you say it's a bindable.
I also assume that there's a reason for actually returning a bindable, rather than a qreal value.
Probably a calculation is implemented.

The code looks safe to me. The only caveat could be, that the propery is actually qreal typed.Depending on how it changes hands in QML, it might become a qreal value at some point, hence loosing the actual binding. But that's not related to your implementation.

I still find it unusual and haven't seen it anywhere else, than in the mentioned example.
Don't fully understand the use case of it. But you sound like you do 🙂, so IMHO there's nothing to worry about.

Cheers,
Axel
________________________________
Von: Interest <interest-bounces at qt-project.org> im Auftrag von Schimkowitsch Robert <Robert.Schimkowitsch at andritz.com>
Gesendet: Freitag, 24. Mai 2024 14:08
An: interest at qt-project.org <interest at qt-project.org>
Betreff: [Interest] Is it safe to construct QBindable from QProperty* via QUntypedBindable?

I use QBindable a lot in C++ classes that expose properties to QML.
My typical property implementation looks as follows:

Q_PROPERTY(qreal x BINDABLE getBindableX READ default FINAL)
//...
QProperty<qreal> m_X;
//...
CPropertyClass::getBindableX() const
{
    return &m_X;
}

I got the idea to use "&m_X" to get the QBindable for a property from the example in QObjectBindableProperty. And initially, I also used QObjectBindableProperty via Q_OBJECT_BINDABLE_PROPERTY macro.
But once QML natively supported BINDABLE properties, I no longer saw any advantage, and using QProperty produces less boilerplate.

So the usage of "&m_X" to get QBindable is documented for QObjectBindableProperty, but only in the example, not in the docs specifically. It feels a bit dirty to create a derived class via promotion from a base class.
And for QProperty, "&m_X" is not documented even in an example.

So the question is: Is it safe to use? Have I misunderstood something by choosing QProperty over Q_OBJECT_BINDABLE_PROPERTY?

Robert Schimkowitsch
________________________________

This message and any attachments are solely for the use of the intended recipients. They may contain privileged and/or confidential information or other information protected from disclosure. If you are not an intended recipient, you are hereby notified that you received this email in error and that any review, dissemination, distribution or copying of this email and any attachment is strictly prohibited. If you have received this email in error, please contact the sender and delete the message and any attachment from your system.

ANDRITZ HYDRO GmbH


Rechtsform/ Legal form: Gesellschaft mit beschränkter Haftung / Corporation

Firmensitz/ Registered seat: Wien

Firmenbuchgericht/ Court of registry: Handelsgericht Wien

Firmenbuchnummer/ Company registration: FN 61833 g

DVR: 0605077

UID-Nr.: ATU14756806


Thank You
________________________________
_______________________________________________
Interest mailing list
Interest at 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/20240524/3a485aba/attachment.htm>


More information about the Interest mailing list