Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Bindings bug or feature

Bindings bug or feature

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 1.4k Views
  • 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.
  • Q Offline
    Q Offline
    QtYury
    wrote on last edited by
    #1

    I found a situation, where bindings works not as I expected. If I bind variable someVar to some other variable(s) and also add handler onSomeVarChanged, the handler executes before bindings. Thus, using any non-trivial code in handler onSomeVarChanged is potentially dangerious. You can use some component, that has variable binded to someVar and this variable is not updated yet. See example below:

    main.qml
    @
    import QtQuick 2.3
    import QtQuick.Controls 1.2
    import QtQuick.Layouts 1.1

    ApplicationWindow {
    id: window

    property var currentDate: null
    
    width: 640
    height: 480
    
    onCurrentDateChanged: {
        print("window.currentDate = " + currentDate)
        print("requestManager.date = " + requestManager.date)
        requestManager.requestData()
    }
    
    RequestManager {
        id: requestManager
        date: currentDate
    }
    
    Button {
        text: "Click Me!"
        onClicked: currentDate = new Date
    }
    

    }
    @

    RequestManager.qml
    @
    import QtQuick 2.0

    Item {

    property var date;
    
    function requestData() {
        print("requesting data for date " + date)
    }
    

    }
    @

    Output (after button click):
    @
    qml: window.currentDate = Пт сен 19 13:26:24 2014 GMT+0300
    qml: requestManager.date = null
    qml: requesting data for date null
    @

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QtYury
      wrote on last edited by
      #2

      Of course, there are a lot of workarounds. For example:

      main.qml
      @
      import QtQuick 2.3
      import QtQuick.Controls 1.2
      import QtQuick.Layouts 1.1

      ApplicationWindow {
      id: window

      property var currentDate: null
      
      width: 640
      height: 480
      
      function setCurrentDate(newDate) {
          currentDate = newDate
          requestManager.requestData()
      }
      
      RequestManager {
          id: requestManager
          date: currentDate
      }
      
      Button {
          text: "Click Me!"
          onClicked: setCurrentDate(new Date)
      }
      

      }
      @

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vincent007
        wrote on last edited by
        #3

        how about this? Is it better?

        @
        ApplicationWindow {
        id: window

        property var currentDate: null
        
        width: 640
        height: 480
        
        RequestManager {
            id: requestManager
            date: currentDate
            onDateChanged: {
                print("window.currentDate = " + currentDate)
                print("requestManager.date = " + date)
                requestData()
            }
        }
        
        Button {
            text: "Click Me!"
            onClicked: currentDate = new Date
        }
        

        }
        @

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          QtYury
          wrote on last edited by
          #4

          Thank you, Vincent. Maybe, it is better in some way.
          But what if we need to execute some code after requestManager.requestData(), for example:

          @
          onCurrentDateChanged: {
          requestManager.requestData()
          viewManager.updateViews()
          // ... etc ...
          }
          @

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chrisadams
            wrote on last edited by
            #5

            Hi,

            One important feature of a declarative language is that you don't know in which order the declarations will be applied in, or the order the side-effects (signal handlers, binding expressions) will be evaluated in. In practice, there is an order defined by the engine implementation, but it is best not to rely on that, as it is an implementation detail rather than a well-specified behaviour (ie, API contract).

            To ensure that an imperative expression is evaluated after a binding evaluation occurs, you can trigger the imperative function via a Timer started as a side-effect of the binding expression.

            Example:

            @
            property bool triggerUpdate: false
            property var someProperty: {
            var newValue = Math.random();
            if (triggerUpdate) {
            timer.start();
            }
            return newValue;
            }
            Timer {
            id: timer
            interval: 0 // will trigger upon returning to the Qt Event Loop
            onTriggered: {
            updateViews() // or whatever
            }
            }
            Component.onCompleted: triggerUpdate = true; // will trigger re-evaluation of someProperty binding, and anything bound to someProperty
            @

            In the preceding case, the updateViews() function is guaranteed to be called after ALL binding expressions involving someProperty have been re-evaluated, since the Timer's onTriggered handler will be invoked at some point in the future via the Qt Event Loop.

            I hope this helps!

            Cheers,
            Chris Adams.

            http://www.qinetic.com.au - The Qt And QML Experts

            1 Reply Last reply
            0

            • Login

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