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. Editing elements of a listmodel -- binding in delegate does not work in both directions ?
Forum Updated to NodeBB v4.3 + New Features

Editing elements of a listmodel -- binding in delegate does not work in both directions ?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 865 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.
  • S Offline
    S Offline
    serdef
    wrote on last edited by
    #1

    Hi,

    I am trying to get an editor working, and have a problem syncing edited values back to the model.
    I hope i am overlooking something simple, maybe you see it ?

    I have listview showing a ListModel. (See a pruned code fragment below)
    The values appear on screen correctly.

    When I edit the 'value' ( MaPropRow is a custom component, but 'value' esssentially maps to a text field),
    this new value is not saved back into the model.

    I update values in the model with a routine 'loadallvalues' when the screen comes up (Component.onCompleted) and this is updated on screen :
    so in this direction binding seems to work : model.value --> value (on screen item in delegate)

    unfortunately, in the other direction it does not : value edited (on screen item in delegate) ---> model.value
    when I edit a 'value' text field, this is not stored back in the model.

    How can I make this work ?

    @

    ListView {
        clip: true
        model: progedit_model
        delegate: progedit_delegate
    }
    Component {
        id: progedit_delegate
        Rectangle{
            height:  prop_rowinterval
            width: parent.width
            color: "transparent"
            MaPropRow{
                y:  (parent.height-height)/2
                width: parent.width
                height: prop_height
                description : model.description
                value : model.value
                unit :  model.unit
                propname: model.propname
                proptype: model.proptype
            }
        }
    }
    
    ListModel {
        id: progedit_model
        // list elements cannot contain qsTr, need to find a solution for translation
          ListElement{
            description : "Cameras"
            value : 1
            unit :  "1 or 3"
            propname: "cameras"
            proptype: "int"
        }
    
        ListElement{
            description : "Density"
            value : 0
            unit :  "_DENSITY"
            propname: "density"
            proptype: "int"
        }
    
        ListElement{
            description : "Compensation"
            value : 0
            unit :  "0.1mm"
            propname: "slipComp"
            proptype: "int"
        }
       
    }
    

    @

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      How are you updating delegate ? MouseArea or TextInput or something else ?You need to catch the appropriate signals of the Delegate and modify the model using setProperty. In my example one of the role is 'name'.

      e.g
      @Component {
      Item {
      TextInput
      {font.pixelSize: 20;
      font.bold: true;
      text : phone
      onEditingFinished:{
      console.log("Here ="+index)
      listView.model.setProperty(index,"name",text)
      }
      }
      }
      ListView {
      id :listView

      }@

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • S Offline
        S Offline
        serdef
        wrote on last edited by
        #3

        Hi,
        thanks a lot
        that makes sense.
        I'll try that.

        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