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. Help with ListModel and setProperty
QtWS25 Last Chance

Help with ListModel and setProperty

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlsetpropertylistmodel
2 Posts 1 Posters 1.3k 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.
  • M Offline
    M Offline
    Mark81
    wrote on 13 Mar 2016, 16:51 last edited by
    #1

    When I try to change a property value of an item contained into a ListModel the following code has no effect:

    Main.qml

    import QtQuick 2.0
    
    Item {
        anchors.fill: parent
    
        ListModel { id: modelCrayon }
    
        Component.onCompleted: {
            for (var i = 0; i < 10; i++)
                modelCrayon.append( { _tag: i, _source: "resources/crayon-green.png", _selected: false } )
        }
    
        Column {
            x: -170
            spacing: 0
            Repeater {
                model: modelCrayon
                delegate: Crayon {
                    tag: _tag
                    source: _source
                    selected: _selected
                    onCrayonSelected: {
                        for (var i = 0; i < modelCrayon.count; i++) {
                            if (i == tag) continue;
                            modelCrayon.setProperty(i, "_selected", false);
                        }
                    }
                }
            }
        }
    }
    

    Crayon.qml

    import QtQuick 2.0
    
    Image {
        property bool selected
        property int tag
        signal crayonSelected()
    
        id: crayon
        smooth: true
        fillMode: Image.PreserveAspectFit
    
        onSelectedChanged: console.debug(tag, selected)
    
        MouseArea {
            anchors.fill: parent
            onClicked: {
                selected = !selected
                if (selected) crayonSelected()
            }
        }
    
        states: State {
            name: "selected"; when: selected == true
            PropertyChanges { target: crayon; x: 30 }
        }
    
        transitions: Transition {
            from: ""; to: "selected"
            PropertyAnimation { property: "x"; duration: 500; easing.type: Easing.InOutQuad }
        }
    
    }
    

    Nothing is shown on console, so the "selected" var is never changed. I'm sure there's something obvious I'm missing.

    By the way, is there a smarter way to use a ListModel as a OptionBox? I mean I want only ONE item at time must have the selected property == true. Or, in other words, keep tracks of the selected index.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mark81
      wrote on 13 Mar 2016, 17:31 last edited by
      #2

      I wasn't able to fix the issue but I solved my problem using a ListView instead of a Column and setting the selected property as ListView.isCurrentItem in the delegate.

      1 Reply Last reply
      0

      1/2

      13 Mar 2016, 16:51

      • Login

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