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

Proposal to add bind-keyword to bind two properties of two objects together.

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Duplicate
    • P4: Low
    • None
    • 4.5.3
    • Core: Object Model
    • None

    Description

      When developing an application with Qt, you usually have a lot of QObjects that to the actual work. Besides you have a graphical user interface that somehow needs to be connected to that core business logic. (If you have a separation between GUI and the rest)

      Qt offers signals and slots as a one way mechanism to send a message from one object to another. For example, an application that controlls PA lightning might have a class Lamp that has three slots: setRed, setGreen, setBlue.

      The user interface might have three sliders, one for each color component. The valueChanged-signals of each of these sliders are then connected to the appropriate slot of the Lamp-instance.

      But if there is more than one way to change the value of each color, for example using a spinbox for precise controll or even there is more than one terminal to controll the lamp, there has to be some kind of feedback mechanism to set the value of the slider from within the lamp.

      There could be of course three signals in the lamp: redChanged, greenChanged, blueChanged and these could be connected to the appropriate setValue-slots of the sliders.

      Then, in an imperative manner, the lamp and the slider would inform each other about changes.

      Wouldn't it be great if the Lamp had three properties: red, green and blue and the sliders could be binded in a declarative way to those properties?

      Imagine besides:
      connect(QObject*, signal, QObject*, slot)
      another method:
      bind(QObject*, property, QObject*, property)
      to syncronize two properties of two QObject.
      For example, bind the value-property of a slider to the red-property of the lamp.

      Besides this new bind-keyword, there should be something analog to connectSlotsByName which connects a bunch of signals to a bunch of slots, that binds bunch of widgets with their user-property to some object with a number of widgets.

      I propose that you can design a form in Qt Designer and set to which property a certain widget shall be bound by a user property called "bind" that has the name of the property to bind to as a value.

      Attached you will find an example programm that shows a demo of such functionality.

      There is a class called BindProxy that implements the proposed bind-property. There is also a class called AbstractController that implements the proposed autoConnect in its bindToView(QWidget* view) method.

      To show what you can do, there is a class called DynamicGuiManager that loads a view from a ui-file and monitors that file and reloads it on change.

      The above mentioned lamp is implemented in the ColorController-class.

      When launching the demo, you will see three controlls. A dial, a slider and a spinbox. Both the slider and the spinbox are bound to the property "red1" of the ColorController. The dial is not.

      Now open ColorWidget.ui in Qt designer while the test application is still open. You will see that both the spinbox and the slider have a dynamic property called bind and have its value set to "red1". If you add such a property to the dial and save the file, the dial will be bound to the property as well and you will imediately see that the dial in the app is moving as well on changing either the slider or the spinbox after saving the ui in designer.

      This bind-keyword is roughly modeled after Cocoa Bindings from Apple. However, it was adopted to fit into Qt and because in Qt, the view has properties as well as the so called controller, it is more abstract than the apple solution because it can be used to tie arbitrary objects together.

      See http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaBindings/Concepts/WhatAreBindings.html#//apple_ref/doc/uid/20002372-CJBEJBHH
      to lern more about cocoa bindings.

      Attachments

        Issue Links

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

          Activity

            People

              Unassigned Unassigned
              axeljaeger Axel Jäger
              Votes:
              2 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes