Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Binding widget value to a variable?



  • Is there any mechanism in Qt to bind a widget's value to a variable so that updating the variable changes the display of the widget and/or changing the widget updates the value of the variable?

    Right now I'm using slots to detect changes in the widgets, manually getting the value of the widget and assigning it to the variable or manually updating the variable and then calling the set function on the widget to update it's display. This feels very clunky and like I might be doing it wrong. Is there some better way to accomplish this that I'm missing?


  • Moderators

    @Dan203 The new Property system in Qt 6 sounds like a good place to start:



  • @JKSH said in Binding widget value to a variable?:

    @Dan203 The new Property system in Qt 6 sounds like a good place to start:

    Is there any way to accomplish something similar in v5? We're using some legacy code which is only 32bit, and since v6 is x64 only we're forced to use v5 until we can get everything upgrade to x64.

    I've been looking at Q_PROPERTY is there some way to use it to link the get/set functions of a widget to a member variable in my dialog class and accomplish basically what I'm looking for here? (the example in the docs isn't the greatest, so it's not 100% clear if this is possible)


  • Moderators

    The traditional Qt way to achieve a "binding" like you described is through signals and slots. The design principles are:

    • Only the setter function is allowed to modify a member variable's value directly. All other functions that wish to change the variable's value must call the setter function.
    • Each time the setter function is called, it should emit a notification signal if the new value is different from the previous value.

    When you follow these principles, you can perform a "binding" by connecting a notification signal of one value to the setter function of the other value, and vice-versa. Something like this:

    #include <QApplication>
    #include <QLineEdit>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QLineEdit l1, l2;
        l1.show();
        l2.show();
    
        QObject::connect(&l1, &QLineEdit::textChanged,
                         &l2, &QLineEdit::setText);
    
        QObject::connect(&l2, &QLineEdit::textChanged,
                         &l1, &QLineEdit::setText);
    
        return a.exec();
    }
    

    Right now I'm using slots to detect changes in the widgets, manually getting the value of the widget and assigning it to the variable or manually updating the variable and then calling the set function on the widget to update it's display.

    Your setter function should emit a notification signal, and you should connect this signal to a slot that updates the widget display.

    Avoid manually assigning to the member variable outside of the setter function. This is because there is no nice way to detect a change caused by manual assignment, so the binding breaks.

    I've been looking at Q_PROPERTY is there some way to use it to link the get/set functions of a widget to a member variable in my dialog class and accomplish basically what I'm looking for here?

    Q_PROPERTY (https://doc.qt.io/qt-5/properties.html ) doesn't really contribute C++ bindings. It does, however, help you bind C++ code to QML code.


Log in to reply