Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Binding widget value to a variable?
Forum Updated to NodeBB v4.3 + New Features

Binding widget value to a variable?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 2.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dan203
    wrote on 14 Mar 2021, 22:30 last edited by
    #1

    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?

    J 1 Reply Last reply 14 Mar 2021, 22:37
    0
    • D Dan203
      14 Mar 2021, 22:30

      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?

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 14 Mar 2021, 22:37 last edited by
      #2

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

      • https://doc.qt.io/qt-6/qproperty.html
      • https://doc.qt.io/qt-6/qobjectbindableproperty.html

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      D 1 Reply Last reply 15 Mar 2021, 21:24
      2
      • J JKSH
        14 Mar 2021, 22:37

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

        • https://doc.qt.io/qt-6/qproperty.html
        • https://doc.qt.io/qt-6/qobjectbindableproperty.html
        D Offline
        D Offline
        Dan203
        wrote on 15 Mar 2021, 21:24 last edited by Dan203
        #3

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

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

        • https://doc.qt.io/qt-6/qproperty.html
        • https://doc.qt.io/qt-6/qobjectbindableproperty.html

        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)

        J 1 Reply Last reply 16 Mar 2021, 02:29
        0
        • D Dan203
          15 Mar 2021, 21:24

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

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

          • https://doc.qt.io/qt-6/qproperty.html
          • https://doc.qt.io/qt-6/qobjectbindableproperty.html

          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)

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 16 Mar 2021, 02:29 last edited by JKSH
          #4

          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.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          2

          1/4

          14 Mar 2021, 22:30

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved