Qt 6.x
The Qt SDK
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)
 See \l QBindable::QBindable(QObject *obj, const char *property)
 
 QBindable (QObject *obj, const char *property)
 Constructs a QBindable for the \l Q_PROPERTY property on obj.
 
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)
};
\inmodule QtCore
Definition qproperty.h:809
\inmodule QtCore
Definition qobject.h:90
GLint GLint GLint GLint GLint x
[0]
#define Q_OBJECT_BINDABLE_PROPERTY(...)
Definition qproperty.h:1237
#define Q_PROPERTY(...)
#define Q_OBJECT
#define signals
p setX(p.x()+1)
qDebug() << bindableX.hasBinding(); // prints false
bindableX.setBinding([&](){ return 2*y.value(); });
qDebug() << bindableX.hasBinding() << myObject->x(); // prints true 84
QBindable< int > bindableX()
QPropertyBinding< T > setBinding(const QPropertyBinding< T > &binding)
Sets the underlying property's binding to binding.
Definition qproperty.h:846
\inmodule QtCore
Definition qproperty.h:349
bool hasBinding() const
Returns true if the underlying property has a binding.
Definition qproperty.h:785
#define qDebug
[1]
Definition qlogging.h:160
GLint y
See also
QMetaProperty::isBindable, QProperty, QObjectBindableProperty, QObjectComputedProperty, {Qt Bindable Properties}

Definition at line 808 of file qproperty.h.

Constructor & Destructor Documentation

◆ QBindable() [1/3]

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

Definition at line 817 of file qproperty.h.

References QUntypedBindable::iface, and QUntypedBindable::metaType().

+ Here is the call graph for this function:

◆ QBindable() [2/3]

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

◆ QBindable() [3/3]

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

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(); });
The QDateTimeEdit class provides a widget for editing dates and times.
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:442
char * toString(const MyType &t)
[31]
See also
QProperty, QObjectBindableProperty, {Qt Bindable Properties}

Definition at line 828 of file qproperty.h.

Member Function Documentation

◆ binding()

template<typename T >
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 835 of file qproperty.h.

References QUntypedBindable::binding().

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

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

◆ makeBinding()

template<typename T >
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 831 of file qproperty.h.

References QUntypedBindable::makeBinding().

+ Here is the call graph for this function:

◆ 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 >
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 692 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 687 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 686 of file qproperty.cpp.

◆ QUntypedBindable() [5/5]

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

Definition at line 682 of file qproperty.h.

◆ setBinding() [1/3]

template<typename T >
template< typename T > void 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 846 of file qproperty.h.

References QBindable< T >::binding(), QUntypedBindable::iface, QtPrivate::BindableWarnings::InvalidInterface, QUntypedBindable::metaType(), QtPrivate::BindableWarnings::printUnsuitableBindableWarning(), Q_ASSERT, QtPrivate::BindableWarnings::ReadOnlyInterface, and QtPrivate::QBindableInterface::setBinding.

Referenced by Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(), and 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 766 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 862 of file qproperty.h.

References Qt::makePropertyBinding(), and QBindable< T >::setBinding().

+ Here is the call graph for this function:

◆ setValue()

template<typename T >
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 883 of file qproperty.h.

References QUntypedBindable::iface, and QtPrivate::QBindableInterface::setter.

◆ takeBinding()

template<typename T >
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 840 of file qproperty.h.

References QUntypedBindable::takeBinding().

+ Here is the call graph for this function:

◆ value()

template<typename T >
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 873 of file qproperty.h.

References QtPrivate::QBindableInterface::getter, and QUntypedBindable::iface.

Friends And Related Symbol Documentation

◆ QPropertyAlias

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

Definition at line 811 of file qproperty.h.


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