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. How to bind a dynamically created QML Element to the ListElement properties in JavaScript
Forum Updated to NodeBB v4.3 + New Features

How to bind a dynamically created QML Element to the ListElement properties in JavaScript

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 10.0k Views 1 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.
  • A Offline
    A Offline
    Aleskey78
    wrote on last edited by
    #1

    I'm creating a custom QML Element derived from TextInput in JavaScript like this:
    @function createItem(index, parent) {
    var root = parent
    while (root !== 'undefined' && !root.hasOwnProperty('model')) {
    root = root.parent
    }

    var model = root.model
    var listelement = model.get(index)
    
    var component = Qt.createComponent(controlFilename);
    if (component.status == Component.Ready) {
           var control = component.createObject(parent);
           control.height= controlHeight
           control.width = controlWidth
           control.anchors.verticalCenter = parent.verticalCenter
           control.anchors.right = parent.right
    
    ...
    
        // binding model.value to control.value
        var newBinding = Qt.createQmlObject('import QtQuick 1.0; Binding { }', parent)
        newBinding.target = control
        newBinding.property = "value"
        newBinding.value = listelement.value
    
        return control;
    }
    

    }
    @

    in TestDelegate.qml
    @
    Component {
    Item {
    id: delegateItem

    ...
    
        Component.onCompleted: {
            JS.createItem(
                index,
                delegateItem)
        }
    

    }
    @

    and the binding does not work...

    But if I declare Binding in QML then it works!
    @
    Component {
    Item {
    id: delegateItem

    ...
    
        // it is ugly!!!
        property variant control: undefined
        Component.onCompleted: {
            control = JS.createItem(
                index,
                delegateItem)
        }
    
        Binding {
            target: control
            property: 'value'
            value: model.value
            //value: delegateItem.parent.parent.model.get(index).value
        }
    

    }
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      minimoog77
      wrote on last edited by
      #2

      See this thread

      http://developer.qt.nokia.com/forums/viewthread/5247/

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        Hi,

        The problem is this line:

        @newBinding.value = listelement.value@

        Which assigns a once-off value to the Binding element's value property (value will be set to the value of listelement.value at the time of the assignment, and never be subsequently updated).

        There are several capabilities coming in QtQuick 1.1 which may be of help, such as the ability to establish bindings from imperative JS code, and the ability to pass property values/bindings to createObject() ( http://doc.qt.nokia.com/4.7-snapshot/qtquick-whatsnew.html ).

        Regards,
        Michael

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Aleskey78
          wrote on last edited by
          #4

          Thanks for your replies.

          Michael:
          in the document you've pointed out said: "Functions can be assigned to properties from JavaScript to create property bindings." - what kind of syntax is used for this? Like normal JS ?: @object.property = function() {...}@

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Aleskey78
            wrote on last edited by
            #5

            Before I'll get Qt 4.7.4, I'm going to use Binding element for dynamic control creation :)
            @Binding {
            id: binding
            target: JS.createItem(
            index,
            delegateItem)
            property: 'value'
            value: model.value
            }@

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbrasser
              wrote on last edited by
              #6

              [quote author="Aleskey78" date="1303974743"]what kind of syntax is used for this? Like normal JS ?: @object.property = function() {...}@
              [/quote]

              Yes, that's correct. If you do a "make docs" in the source tree, you should be able to see the documentation for this -- I'm still trying to track down why it isn't showing up online for the 4.7-snapshot docs.

              Regards,
              Michael

              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