Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-24385

Widgets which can get bound to properties

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • 4.8.0
    • None

    Description

      Use case:
      Signals and slots in the Qt library are a great feature. But developers of the library missed an important need. They show how to connect two UI elements graphically but they missed that in most data based application the connection from UI elements to data containers and back is much more important.

      This has to be done manually and it is a very repetitive task. You have to raise a signal if your property changed, connect this signal to your UI element. And you have to connect to the signal of the UI element to automatically update your property.

      Much easier is a "binding" which just created a "two way" connection between the UI element and the property in the data container.

      Feature Request:

      • Introduce the concept of a "binding" into the Qt library as generic as possible. A binding is a two way connection between an object and a property.
      • Extend the QWidget's with values which can get bound to properties. For example the value itself, but also other properties like the "isEnabled" or other relevant values.
      • Also keep in mind arrays of objects with properties, which are covered by the binding mechanishm. They allow a simple binding of arrays to QComboBox, QListWidget etc.

      Example solution

      See the feature suggestion QTBUG-24381 which describes how to automate the implementation of properties with notification. This makes the properties generic and usable for data binding.

      An abstract base class for all "bound" widgets:

      class BoundWidget
      {
      public:
      virtual void bindToValue( dzcore::BindableValue *bindableValue, const QString &valuePath ) = 0;
      virtual void releaseFromValue() = 0;
      };
      

      With "bindToValue" you can bind the widget to a data container which is supporting the "BindableValue" interface. A "value path" is used to access the property which can also be in some sub container in the data hierarchy.

      Bindable versions of the most important Qt widgets:

      class BoundSpinBox : public QSpinBox, public BoundWidget
      {
      // ...
      

      The BindableValue interface looks like this:

      class BindableValue : public QObject
      {
      // ...
      public:
      virtual QVariant valueAtPath( const QString &valuePath ) const = 0;
      virtual bool setValueAtPath( const QString &valuePath, const QVariant &value ) = 0;
      signals:
      void valueAtPathChanged( const QString &valuePath, const QVariant &newValue );
      };
      

      The UI element also automatically detects if the QObject to which it is bound is deleted and just releases the binding.

      You can also specify for each element, what happens when the element has no binding or if the value does not meet the criteria of the element. For example the element can get disabled in this cases.

      A real world example is this preference dialog, which just has to call a few methods in the initialization method:

      void PreferencesDialog::initializeUi()
      {
      dzconf::ModulePtr preferencesModule = dzconf::gManager()->preferences();
      
      ui.showConfirmDialog->bindToValue( preferencesModule.data(), "showConfirmOnSave" );
      ui.numSelectedDeltaVersions->bindToValue( preferencesModule.data(), "numSelectedDeltaVersions" );
      ui.showStoredVersionInChannel->bindToValue( preferencesModule.data(), "showStoredVersionInChannel" );
      ui.showSaveButtonInChannel->bindToValue( preferencesModule.data(), "showSaveButtonInChannel" );
      }
      

      This is the only code in this dialog, and everything works automatically.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            qtcomsupport Qt Support
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes