Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QBindable< T > Class Template Reference

\inmodule QtCore More...

#include <qproperty.h>

+ Inheritance diagram for QBindable< T >:
+ Collaboration diagram for QBindable< T >:

Public Member Functions

 QBindable (const QUntypedBindable &b)
 
 QBindable (QObject *obj, const QMetaProperty &property)
 
 QBindable (QObject *obj, const char *property)
 
QPropertyBinding< T > makeBinding (const QPropertyBindingSourceLocation &location=QT_PROPERTY_DEFAULT_BINDING_LOCATION) const
 Constructs a binding evaluating to the underlying property's value, using a specified source location.
 
QPropertyBinding< T > binding () const
 Returns the currently set binding of the underlying property.
 
QPropertyBinding< T > takeBinding ()
 Removes the currently set binding of the underlying property and returns it.
 
QPropertyBinding< T > setBinding (const QPropertyBinding< T > &binding)
 Sets the underlying property's binding to binding.
 
template<typename Functor >
QPropertyBinding< T > setBinding (Functor &&f, const QPropertyBindingSourceLocation &location=QT_PROPERTY_DEFAULT_BINDING_LOCATION, std::enable_if_t< std::is_invocable_v< Functor > > *=nullptr)
 
value () const
 Returns the underlying property's current value.
 
void setValue (const T &value)
 Sets the underlying property's value to value.
 
constexpr QUntypedBindable (QUntypedPropertyData *d, const QtPrivate::QBindableInterface *i)
 
Q_CORE_EXPORT QUntypedBindable (QObject *obj, const QMetaProperty &property, const QtPrivate::QBindableInterface *i)
 
Q_CORE_EXPORT QUntypedBindable (QObject *obj, const char *property, const QtPrivate::QBindableInterface *i)
 
constexpr QUntypedBindable ()=default
 Default-constructs a QUntypedBindable.
 
template<typename Property >
 QUntypedBindable (Property *p)
 Constructs a QUntypedBindable from the property property.
 
bool setBinding (const QUntypedPropertyBinding &binding)
 Sets the underlying property's binding to binding.
 
- Public Member Functions inherited from QUntypedBindable
constexpr QUntypedBindable ()=default
 Default-constructs a QUntypedBindable.
 
template<typename Property >
 QUntypedBindable (Property *p)
 Constructs a QUntypedBindable from the property property.
 
bool isValid () const
 Returns true if the QUntypedBindable is valid.
 
bool isBindable () const
 
bool isReadOnly () const
 
QUntypedPropertyBinding makeBinding (const QPropertyBindingSourceLocation &location=QT_PROPERTY_DEFAULT_BINDING_LOCATION) const
 Creates a binding returning the underlying properties' value, using a specified source location.
 
QUntypedPropertyBinding takeBinding ()
 Removes the currently set binding from the property and returns it.
 
void observe (QPropertyObserver *observer) const
 
template<typename Functor >
QPropertyChangeHandler< FunctoronValueChanged (Functor f) const
 Installs f as a change handler.
 
template<typename Functor >
QPropertyChangeHandler< Functorsubscribe (Functor f) const
 Behaves like a call to f followed by onValueChanged(f),.
 
template<typename Functor >
QPropertyNotifier addNotifier (Functor f)
 Installs f as a change handler.
 
QUntypedPropertyBinding binding () const
 Returns the underlying property's binding if there is any, or a default constructed QUntypedPropertyBinding otherwise.
 
bool setBinding (const QUntypedPropertyBinding &binding)
 Sets the underlying property's binding to binding.
 
bool hasBinding () const
 Returns true if the underlying property has a binding.
 
QMetaType metaType () const
 

Friends

template<typename U >
class QPropertyAlias
 

Additional Inherited Members

- Protected Member Functions inherited from QUntypedBindable
constexpr QUntypedBindable (QUntypedPropertyData *d, const QtPrivate::QBindableInterface *i)
 
Q_CORE_EXPORT QUntypedBindable (QObject *obj, const QMetaProperty &property, const QtPrivate::QBindableInterface *i)
 
Q_CORE_EXPORT QUntypedBindable (QObject *obj, const char *property, const QtPrivate::QBindableInterface *i)
 
- Protected Attributes inherited from QUntypedBindable
QUntypedPropertyDatadata = nullptr
 
const QtPrivate::QBindableInterfaceiface = nullptr
 

Detailed Description

template<typename T>
class QBindable< T >

\inmodule QtCore

QBindable is a wrapper class around binding-enabled properties. It allows type-safe operations while abstracting the differences between the various property classes away. \inherits QUntypedBindable

QBindable<T> helps to integrate Qt's traditional Q_PROPERTY with \l {Qt Bindable Properties}{binding-enabled} properties. If a property is backed by a QProperty, QObjectBindableProperty or QObjectComputedProperty, you can add BINDABLE bindablePropertyName to the Q_PROPERTY declaration, where bindablePropertyName is a function returning an instance of QBindable constructed from the QProperty. The returned QBindable allows users of the property to set and query bindings of the property, without having to know the exact kind of binding-enabled property used.

class MyClass : public QObject
{
Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
public:
int x() const { return xProp; }
void setX(int x) { xProp = x; }
QBindable<int> bindableX() { return QBindable<int>(&xProp); }
void xChanged();
private:
// Declare the instance of the bindable property data.
Q_OBJECT_BINDABLE_PROPERTY(MyClass, int, xProp, &MyClass::xChanged)
};
qDebug() << bindableX.hasBinding(); // prints false
bindableX.setBinding([&](){ return 2*y.value(); });
qDebug() << bindableX.hasBinding() << myObject->x(); // prints true 84
See also
QMetaProperty::isBindable, QProperty, QObjectBindableProperty, QObjectComputedProperty, {Qt Bindable Properties}

Definition at line 811 of file qproperty.h.

Constructor & Destructor Documentation

◆ QBindable() [1/3]

template<typename T >
QBindable< T >::QBindable ( const QUntypedBindable & b)
inlineexplicit

Definition at line 820 of file qproperty.h.

References QUntypedBindable::iface.

◆ QBindable() [2/3]

template<typename T >
QBindable< T >::QBindable ( QObject * obj,
const QMetaProperty & property )
inlineexplicit
Since
6.5

See \l QBindable::QBindable(QObject *obj, const char *property)

Definition at line 828 of file qproperty.h.

◆ QBindable() [3/3]

template<typename T >
QBindable< T >::QBindable ( QObject * obj,
const char * property )
inlineexplicit
Since
6.5

Constructs a QBindable for the \l Q_PROPERTY property on obj. The property must have a notify signal but does not need to have BINDABLE in its Q_PROPERTY definition, so even binding unaware {Q_PROPERTY}s can be bound or used in binding expressions. You must use QBindable::value() in binding expressions instead of the normal property READ function (or MEMBER) to enable dependency tracking if the property is not BINDABLE. When binding using a lambda, you may prefer to capture the QBindable by value to avoid the cost of calling this constructor in the binding expression. This constructor should not be used to implement BINDABLE for a Q_PROPERTY, as the resulting Q_PROPERTY will not support dependency tracking. To make a property that is usable directly without reading through a QBindable use \l QProperty or \l QObjectBindableProperty.

QProperty<QString> displayText;
QDateTimeEdit *dateTimeEdit = findDateTimeEdit();
QBindable<QDateTime> dateTimeBindable(dateTimeEdit, "dateTime");
displayText.setBinding([dateTimeBindable](){ return dateTimeBindable.value().toString(); });
\inmodule QtCore
Definition qproperty.h:812
The QDateTimeEdit class provides a widget for editing dates and times.
\inmodule QtCore
Definition qproperty.h:352
QPropertyBinding< T > setBinding(const QPropertyBinding< T > &newBinding)
Associates the value of this property with the provided newBinding expression and returns the previou...
Definition qproperty.h:445
See also
QProperty, QObjectBindableProperty, {Qt Bindable Properties}

Definition at line 831 of file qproperty.h.

Member Function Documentation

◆ binding()

template<typename T >
QPropertyBinding< T > QBindable< T >::binding ( ) const
inline

Returns the currently set binding of the underlying property.

If the property does not have a binding, the returned QPropertyBinding<T> will be invalid.

See also
setBinding, hasBinding !
QPropertyBinding::isValid()

Definition at line 838 of file qproperty.h.

◆ makeBinding()

template<typename T >
QPropertyBinding< T > QBindable< T >::makeBinding ( const QPropertyBindingSourceLocation & location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) const
inline

Constructs a binding evaluating to the underlying property's value, using a specified source location.

Definition at line 834 of file qproperty.h.

◆ QUntypedBindable() [1/5]

template<typename T >
QUntypedBindable::QUntypedBindable ( )
constexprdefault

Default-constructs a QUntypedBindable.

It is in an invalid state.

See also
isValid()

◆ QUntypedBindable() [2/5]

template<typename T >
template<typename Property >
QUntypedBindable::QUntypedBindable ( Property * property)
inline

Constructs a QUntypedBindable from the property property.

If Property is const, the QUntypedBindable will be read only. If property is null, the QUntypedBindable will be invalid.

See also
isValid(), isReadOnly()

Definition at line 695 of file qproperty.h.

◆ QUntypedBindable() [3/5]

template<typename T >
QUntypedBindable::QUntypedBindable ( QObject * obj,
const char * property,
const QtPrivate::QBindableInterface * i )
explicit

Definition at line 690 of file qproperty.cpp.

◆ QUntypedBindable() [4/5]

template<typename T >
QUntypedBindable::QUntypedBindable ( QObject * obj,
const QMetaProperty & property,
const QtPrivate::QBindableInterface * i )
explicit

Definition at line 689 of file qproperty.cpp.

◆ QUntypedBindable() [5/5]

template<typename T >
QUntypedBindable::QUntypedBindable ( QUntypedPropertyData * d,
const QtPrivate::QBindableInterface * i )
inlineconstexpr

Definition at line 685 of file qproperty.h.

◆ setBinding() [1/3]

template<typename T >
QPropertyBinding< T > QBindable< T >::setBinding ( const QPropertyBinding< T > & binding)
inline

Sets the underlying property's binding to binding.

Does nothing if the QBindable is read-only or invalid.

See also
binding, isReadOnly(), isValid() !
QPropertyBinding::isValid()

Definition at line 849 of file qproperty.h.

References QUntypedBindable::iface, QtPrivate::BindableWarnings::InvalidInterface, QtPrivate::BindableWarnings::printUnsuitableBindableWarning(), and QtPrivate::BindableWarnings::ReadOnlyInterface.

Referenced by QBindable< T >::setBinding().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setBinding() [2/3]

template<typename T >
bool QUntypedBindable::setBinding ( const QUntypedPropertyBinding & binding)
inline

Sets the underlying property's binding to binding.

This does not have any effect if the QUntypedBindable is read-only, null or if binding's type does match the underlying property's type.

Returns
true when the binding was successfully set.

!

See also
QUntypedPropertyBinding::valueMetaType()

Definition at line 769 of file qproperty.h.

◆ setBinding() [3/3]

template<typename T >
template<typename Functor >
QPropertyBinding< T > QBindable< T >::setBinding ( Functor && f,
const QPropertyBindingSourceLocation & location = QT_PROPERTY_DEFAULT_BINDING_LOCATION,
std::enable_if_t< std::is_invocable_v< Functor > > * = nullptr )
inline

Definition at line 865 of file qproperty.h.

References QBindable< T >::setBinding().

+ Here is the call graph for this function:

◆ setValue()

template<typename T >
void QBindable< T >::setValue ( const T & value)
inline

Sets the underlying property's value to value.

This removes any currenltly set binding from it. This function has no effect if the QBindable is read-only or invalid.

See also
isValid(), isReadOnly(), setBinding()

Definition at line 886 of file qproperty.h.

◆ takeBinding()

template<typename T >
QPropertyBinding< T > QBindable< T >::takeBinding ( )
inline

Removes the currently set binding of the underlying property and returns it.

If the property does not have a binding, the returned QPropertyBinding<T> will be invalid.

See also
binding, setBinding, hasBinding !
QPropertyBinding::isValid()

Definition at line 843 of file qproperty.h.

◆ value()

template<typename T >
T QBindable< T >::value ( ) const
inline

Returns the underlying property's current value.

If the QBindable is invalid, a default constructed T is returned.

See also
isValid()

Definition at line 876 of file qproperty.h.

References QUntypedBindable::iface.

Friends And Related Symbol Documentation

◆ QPropertyAlias

template<typename T >
template<typename U >
friend class QPropertyAlias
friend

Definition at line 814 of file qproperty.h.


The documentation for this class was generated from the following files: