-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Research how to implement Qt style property bindings #132
Comments
Hi, author of bindable_properties here. First of all, great work you've done here. I wish I found this library sooner. Qt runs the lambda when the binding is first set, and then stores all properties whose "value" function was called. I'm using the same technique. I'm very surprised by your example not showing this. #include <QProperty>
#include <assert.h>
int main() {
QProperty<int> prop1 { 3 };
QProperty<int> prop2 { 4 };
QProperty<int> prop3;
bool prop3_bound_to_prop1 = true;
prop3.setBinding([&]() {
if (prop3_bound_to_prop1) {
return prop1.value();
} else {
return prop2.value();
}
});
prop1.setValue(5);
assert(prop3.value() == 5);
prop3_bound_to_prop1 = false;
prop2.setValue(6);
assert(prop3.value() == 6); // this fails
} I think it's a really neat trick, but it has the unintended problem you mentioned of not capturing properties in code branches that didn't run. AFAIK Qt doesn't warn about this (neither do I). |
#include <QtCore/QDebug>
#include <QtCore/QProperty>
#define LOGGED_EXPR(_EXPR_) \
qDebug() << "===================\n" << #_EXPR_; \
_EXPR_
void condition_qproperty() {
QProperty<int> left(-1);
QProperty<int> right(1);
QProperty<bool> is_right(true);
QProperty<int> result;
result.setBinding([&]() {
qDebug() << "result is calculated";
if (is_right.value())
return right.value();
else
return left.value();
});
auto wtf = result.subscribe(
[&]() { qDebug() << "result is notified: " << result.value(); });
LOGGED_EXPR(right = 2);
LOGGED_EXPR(left = -2);
LOGGED_EXPR(is_right = false);
LOGGED_EXPR(left = 3);
}
void condition_bool() {
QProperty<int> left(-1);
QProperty<int> right(1);
bool is_right(true);
QProperty<int> result;
result.setBinding([&]() {
qDebug() << "result is calculated";
if (is_right)
return right.value();
else
return left.value();
});
auto wtf = result.subscribe(
[&]() { qDebug() << "result is notified: " << result.value(); });
LOGGED_EXPR(right = 2);
LOGGED_EXPR(left = -2);
LOGGED_EXPR(is_right = false);
LOGGED_EXPR(left = 3);
}
int main() {
qDebug() << "\n------- condition_qproperty ------\n";
condition_qproperty();
qDebug() << "\n--------- condition_bool ---------\n";
condition_bool();
} It seems that dependency of non-property condition (condition_bool) breaks the system (but it is bad practice in general, at least because non-property has no ways to notify property to update it. And yes, there is no any way to detect and warn it from code. Also, it seems that used properties are detected not only on the first calculation, but on each of them. In the condition_qproperty example, It is more or less easy to implement. I feared, that it requires some black magic, when I saw P.S. But my idea of using property bindings for |
Description
Property bindings is an approach intended to minimize duplication in property/signal lifting. Ureact used overloaded operators to dedup expressions, but unfortunately it comes with a cost with complex expressions. It's faster to wrap all expression in a lambda, than writing it as expression:
Compilation times and runtime speed of d is better. But we have to repeat x and y three times instead of one. Property bindings can solve it:
It might improve not only lift, but
synced
versions oftransform
,filter
etc too.The problem is - I have no idea how to implement it. So I need to make some research.
See https://doc.qt.io/qt-6/bindableproperties.html
As I understand, Qt has some lambda capture mechanism that allows to see which QProperty are captured and used. Not only types, but specific values.
Alternative approach is - run lambda once and detect which QProperty::value() were called. But it is not the case for Qt. I checked it on
if( flag.value ) return a.value(); else return b.value();
and Qt still captured all 3 variables.Similar approach is implemented in https://github.com/theartful/bindable_properties:
The text was updated successfully, but these errors were encountered: