An example of how to use KDBindings::BindingEvaluator together with a KDBindings::Property to create a Property binding that is only reevaluated on demand.
The output of this example is:
The initial size of the image = 1920000 bytes
The new size of the image = 8294400 bytes
Note the difference to 05-property-bindings/main.cpp, where the new size of the image is calculated twice.
This feature is especially useful to reduce the performance impact of bindings and to create bindings that only update in specific intervals.
#include <iostream>
static BindingEvaluator evaluator;
class Image
{
public:
const int bytesPerPixel = 4;
Property<int> width{ 800 };
Property<int> height{ 600 };
const Property<int> byteSize =
makeBoundProperty(evaluator, bytesPerPixel *width *height);
};
int main()
{
Image img;
std::cout << "The initial size of the image = " << img.byteSize.get() << " bytes" << std::endl;
(void)img.byteSize.valueChanged().connect([](const int &newValue) {
std::cout << "The new size of the image = " << newValue << " bytes" << std::endl;
});
img.width = 1920;
img.height = 1080;
evaluator.evaluateAll();
return 0;
}
The main namespace of the KDBindings library.
auto makeBoundProperty(T &&...args)
Helper function to create a Property with a Binding.