QBindable Class▲
-
Header: QBindable
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Inherits: QUntypedBindable
-
Group: QBindable is part of tools
Detailed Description▲
QBindable<T> helps to integrate Qt's traditional Q_PROPERTY with 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_OBJECT
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&
lt;int
&
gt; bindableX() {
return
QBindable&
lt;int
&
gt;(&
amp;xProp); }
signals
:
void
xChanged();
private
:
// Declare the instance of the bindable property data.
Q_OBJECT_BINDABLE_PROPERTY(MyClass, int
, xProp, &
amp;MyClass::
xChanged)
}
;
MyClass *
myObject;
QBindable&
lt;int
&
gt; bindableX =
myObject-&
gt;bindableX();
qDebug() &
lt;&
lt; bindableX.hasBinding(); // prints false
QProperty&
lt;int
&
gt; y {
42
}
;
bindableX.setBinding([&
amp;](){
return
2
*
y.value(); }
);
qDebug() &
lt;&
lt; bindableX.hasBinding() &
lt;&
lt; myObject-&
gt;x(); // prints true 84
See Also▲
Member Function Documentation▲
QPropertyBinding<T> QBindable::binding() const▲
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▲
See also setBinding, hasBinding
QPropertyBinding<T> QBindable::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> QBindable::setBinding(const QPropertyBinding<T> &binding)▲
Sets the underlying property's binding to binding. Does nothing if the QBindable is read-only or invalid.
See Also▲
See also binding, isReadOnly(), isValid()
QPropertyBinding<T> QBindable::setBinding(Functor f)▲
This is an overloaded function.
Creates a QPropertyBinding<T> from f, and sets it as the underlying property's binding.
void QBindable::setValue(const T &value)▲
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▲
See also value(), isValid(), isReadOnly(), setBinding()
QPropertyBinding<T> QBindable::takeBinding()▲
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▲
See also binding, setBinding, hasBinding