Conversion from QProperty to QBinding
-
I have adopted the following pattern when writing my QML-facing C++ property classes:
class CProp : public QObject { Q_OBJECT QML_ELEMENT QML_UNCREATABLE("Controlled by C++") Q_PROPERTY(qreal x BINDABLE getBindableX READ default FINAL) private: QProperty<qreal> m_X; public: QBindable<qreal> getBindableX() { return &m_X;} };
It's easy to write, it's convenient, and it seems to work.
Two things trouble me:- The conversion from QProperty to QBindable via '&' does not seem to be documented. I must have taken it from some blog post or example, but I'm worried that I use an unsupported implicit conversion that might go away in the future
- Could the getter be const? Does a QBindable allow changing the property within?
-
So I suspect things work because QUntypedBindable (base class of QBindable) has a constructor accepting a template parameter pointer ("Property"), so my guess is that "&m_X" provides a pointer to QProperty<qreal>, which is accepted as template parameter in the constructor of QUntypedBindable. Then the whole thing is cast to QBindable, but is that safe?