QPropertyAlias Class
The QPropertyAlias class is a safe alias for a QProperty with same template parameter. More...
Header: | #include <QPropertyAlias> |
Inherits: | QPropertyObserver |
Public Functions
QPropertyAlias(QProperty<T> *property) | |
QPropertyAlias(QPropertyAlias<T> *alias) | |
QPropertyNotifier | addNotifier(Functor f) |
QPropertyBinding<T> | binding() const |
bool | hasBinding() const |
bool | isValid() const |
QPropertyChangeHandler<Functor> | onValueChanged(Functor f) |
QPropertyBinding<T> | setBinding(const QPropertyBinding<T> &newBinding) |
bool | setBinding(const QUntypedPropertyBinding &newBinding) |
QPropertyBinding<T> | setBinding(Functor f) |
void | setValue(const T &newValue) |
QPropertyChangeHandler<Functor> | subscribe(Functor f) |
QPropertyBinding<T> | takeBinding() |
T | value() const |
T | operator T() const |
QPropertyAlias<T> & | operator=(const T &newValue) |
Detailed Description
\inmodule
QtCore \internal
\ingroup
tools
QPropertyAlias<T> wraps a pointer to a QProperty<T> and automatically invalidates itself when the QProperty<T> is destroyed. It forwards all method invocations to the wrapped property. For example:
QProperty<QString> *name = new QProperty<QString>("John"); QProperty<int> age(41); QPropertyAlias<QString> nameAlias(name); QPropertyAlias<int> ageAlias(&age); QProperty<QString> fullname; fullname.setBinding([&]() { return nameAlias.value() + " age: " + QString::number(ageAlias.value()); }); qDebug() << fullname.value(); // Prints "John age: 41" *name = "Emma"; // Marks binding expression as dirty qDebug() << fullname.value(); // Re-evaluates the binding expression and prints "Emma age: 41" // Birthday is coming up ageAlias.setValue(age.value() + 1); // Writes the age property through the alias qDebug() << fullname.value(); // Re-evaluates the binding expression and prints "Emma age: 42" delete name; // Leaves the alias in an invalid, but accessible state nameAlias.setValue("Eve"); // Ignored: nameAlias carries a default-constructed QString now ageAlias.setValue(92); qDebug() << fullname.value(); // Re-evaluates the binding expression and prints " age: 92"
Member Function Documentation
QPropertyAlias::QPropertyAlias(QProperty<T> *property)
Constructs a property alias for the given property.
QPropertyAlias::QPropertyAlias(QPropertyAlias<T> *alias)
Constructs a property alias for the property aliased by alias.
template <typename Functor> QPropertyNotifier QPropertyAlias::addNotifier(Functor f)
Subscribes the given functor f as a callback that is called whenever the value of the aliased property changes.
The callback f is expected to be a type that has a plain call operator ()
without any parameters. This means that you can provide a C++ lambda expression, a std::function or even a custom struct with a call operator.
The returned property change handler object keeps track of the subscription. When it goes out of scope, the callback is unsubscribed.
This method is in some cases easier to use than onValueChanged(), as the returned object is not a template. It can therefore more easily be stored, e.g. as a member in a class.
See also onValueChanged() and subscribe().
QPropertyBinding<T> QPropertyAlias::binding() const
Returns the binding expression that is associated with the aliased property. A default constructed QPropertyBinding<T> will be returned if no such association exists.
See also setBinding().
bool QPropertyAlias::hasBinding() const
Returns true if the aliased property is associated with a binding; false otherwise.
bool QPropertyAlias::isValid() const
Returns true if the aliased property still exists; false otherwise.
If the aliased property doesn't exist, all other method calls are ignored.
template <typename Functor> QPropertyChangeHandler<Functor> QPropertyAlias::onValueChanged(Functor f)
Registers the given functor f as a callback that shall be called whenever the value of the aliased property changes. On each value change, the handler is either called immediately, or deferred, depending on the context.
The callback f is expected to be a type that has a plain call operator ()
without any parameters. This means that you can provide a C++ lambda expression, a std::function or even a custom struct with a call operator.
The returned property change handler object keeps track of the registration. When it goes out of scope, the callback is de-registered.
QPropertyBinding<T> QPropertyAlias::setBinding(const QPropertyBinding<T> &newBinding)
Associates the value of the aliased property with the provided newBinding expression and returns any previous binding the associated with the aliased property.The property's value is set to the result of evaluating the new binding. Whenever a dependency of the binding changes, the binding will be re-evaluated, and the property's value gets updated accordingly.
Returns any previous binding associated with the property, or a default-constructed QPropertyBinding<T>.
See also binding().
bool QPropertyAlias::setBinding(const QUntypedPropertyBinding &newBinding)
This is an overloaded function.
Associates the value of the aliased property with the provided newBinding expression. The property's value is set to the result of evaluating the new binding. Whenever a dependency of the binding changes, the binding will be re-evaluated, and the property's value gets updated accordingly.
Returns true if the type of this property is the same as the type the binding function returns; false otherwise.
template <typename Functor> QPropertyBinding<T> QPropertyAlias::setBinding(Functor f)
This is an overloaded function.
Associates the value of the aliased property with the provided functor f expression. The property's value is set to the result of evaluating the new binding. Whenever a dependency of the binding changes, the binding will be re-evaluated, and the property's value gets updated accordingly.
Returns any previous binding associated with the property, or a default-constructed QPropertyBinding<T>.
See also Formulating a Property Binding.
void QPropertyAlias::setValue(const T &newValue)
Assigns newValue to the aliased property and removes the property's associated binding, if present.
See also value().
template <typename Functor> QPropertyChangeHandler<Functor> QPropertyAlias::subscribe(Functor f)
Subscribes the given functor f as a callback that is called immediately and whenever the value of the aliased property changes in the future. On each value change, the handler is either called immediately, or deferred, depending on the context.
The callback f is expected to be a type that has a plain call operator ()
without any parameters. This means that you can provide a C++ lambda expression, a std::function or even a custom struct with a call operator.
The returned property change handler object keeps track of the subscription. When it goes out of scope, the callback is unsubscribed.
QPropertyBinding<T> QPropertyAlias::takeBinding()
Disassociates the binding expression from the aliased property and returns it. After calling this function, the value of the property will only change if you assign a new value to it, or when a new binding is set.
T QPropertyAlias::value() const
Returns the value of the aliased property. This may evaluate a binding expression that is tied to the property, before returning the value.
See also setValue().
T QPropertyAlias::operator T() const
Returns the value of the aliased property. This may evaluate a binding expression that is tied to the property, before returning the value.
QPropertyAlias<T> &QPropertyAlias::operator=(const T &newValue)
Assigns newValue to the aliased property and returns a reference to this QPropertyAlias.