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

Schimkowitsch Robert Robert.Schimkowitsch at andritz.com
Fri May 24 20:44:47 CEST 2024


Sorry, the code was incomplete, it should have been

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

The purpose is to transmit data from the C++ backend to the QML front end. It’s a one-way road, QML only has to read and update when properties change.
I have hundreds of such properties in dozens of classes, so I was looking for the simplest possible way to get it done. Before 6.5, code was more like this

Q_PROPERTY(qreal x READ getX NOTIFY notifyXChanged FINAL)
//..
qreal m_X;
//...
signals:
   void notifyXChanged();
//...
qreal getX() const
{
   return m_X;
}
//...
void setX(const qreal arg)
{
   if (m_X == arg)
   {
      return;
   }
   m_X = arg;
   emit notifyXChanged();
}

Since 6.5, using BINDABLE keyword and READ DEFAULT is sufficient for QML to update, and as you can see, the code is much simpler. In the older version, I need a setter (which is not reachable from QML) because of the change comparison. The QProperty/QBindable implements that for me: If I set a property to the same value, no updates are triggered.

Hope that helps making things clear.

Kind regards

Robert

From: Interest <interest-bounces at qt-project.org> On Behalf Of Axel Spoerl via Interest
Sent: Friday, 24 May 2024 15:02
To: interest at qt-project.org
Subject: Re: [Interest] Is it safe to construct QBindable from QProperty* via QUntypedBindable?

CAUTION: External email. Do not click on links or open attachments unless you know the sender and that the content is safe.

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<mailto:interest-bounces at qt-project.org>> im Auftrag von Schimkowitsch Robert <Robert.Schimkowitsch at andritz.com<mailto:Robert.Schimkowitsch at andritz.com>>
Gesendet: Freitag, 24. Mai 2024 14:08
An: interest at qt-project.org<mailto:interest at qt-project.org> <interest at qt-project.org<mailto: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<mailto:Interest at qt-project.org>
https://lists.qt-project.org/listinfo/interest<https://ddec1-0-en-ctp.trendmicro.com:443/wis/clicktime/v1/query?url=https%3a%2f%2flists.qt%2dproject.org%2flistinfo%2finterest&umid=034e84fd-abed-4fb1-adcb-fe8f920c4230&auth=c40ca16f35916d8a02f8c25adf579f293a3f6a33-79ebfaff5daa65baab8bc9a41a36c18be62c5eb2>
________________________________

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
________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20240524/c9870746/attachment.htm>


More information about the Interest mailing list