7

In QML, it's easy write create a property binding, such as:

Rectangle { width: parent.width } 

Is it possible to do this in C++ too?

1
  • There is no C++ API to do it directly, you have to make a connection from the parent's widthChanged signal to say a lambda that sets the width to the parent width. Commented Sep 29, 2015 at 15:30

2 Answers 2

7

In Qt, some QObjects have certain properties that can be "bound" using signals and slots:

auto *someWidget = QPushButton(/* ... */); auto *otherRelatedWidget = QLabel( /* ... */ ); // windowTitle is a property for both QWidgets QObject::connect(someWidget, &QWidget::windowTitleChanged, otherRelatedWidget, &QWidget::setWindowTitle); 

Apart from this, you can still connect other signals and slots, even if they're not associated to properties.

I've got to point out that there is no syntax sugar for doing this. See the properties documentation for more info.

Sign up to request clarification or add additional context in comments.

Comments

2

In Qt6 you can use QProperty to achieve c++ property bindings.

Check out this blog post: https://www.qt.io/blog/property-bindings-in-qt-6

They're slightly different than QML binding since they execute lazily. QML binding execute eagerly on every signal. Under the hood QProperty uses thread local storage (TLS) to keep track of dependancies while executing value(). It's definitely an interesting piece of technology and adds a declarative programming paradigm to c++.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.