[Qt-qml] Models and binding values

alan.westbrook at nokia.com alan.westbrook at nokia.com
Thu Jul 22 22:19:48 CEST 2010


Just to make sure that it's not something complex that's happening, I put together this simple test case (adapted from the documentation about models):


    ListModel {

        id: fruitModel

        ListElement {

            name: "Apple"

            cost: 2.45

        }

        ListElement {

            name: "Orange"

            cost: 3.25

        }

        ListElement {

            name: "Banana"

            cost: 1.95

        }

    }

    ListView {

        x: 10

        y: 200

        width: 200; height: 250

        model: fruitModel

        delegate: Row {

            Text { text: "Fruit: " + name }

            Text { text: "Cost: $" + cost }

        }

        MouseArea {

            anchors.fill: parent

            onClicked: { fruitModel.get(0).name = "bob"; console.log("ListElement.name<http://ListElement.name>: " + fruitModel.get(0).name) }

        }

    }

The elements created by the delegate do not update when the model data is changed.

Please forgive the colors, just imagine the background is black (and the black text color is white). =)

The doc says: "Data is provided to the delegate via named data roles which the delegate may bind to."
from: http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodels.html

Is this a bug, or am I missing something?

Thanks!
Alan

On Jul 22, 2010, at 11:39 AM, ext alan.westbrook at nokia.com<mailto:alan.westbrook at nokia.com> wrote:

Hi,

The docs mention it, but I can't seem to get it to work.

I am trying to get items in a list to bind to values from the model that is assigned.

Here are some things I have tried:


    Component {

        id: bardelegate

        Rectangle {

                ...

            Rectangle {

                id: self

                ...

                opacity: selected ? 0.6 : 1.0


                property bool itemSelected: model.selected

                onItemSelectedChanged: opacity = itemSelected ? 0.6 : 1.0

                Binding { target: self; property: "itemSelected"; value: model.selected }


                states: [

                    State {

                        name: "selected"

                        when: itemSelected

                        PropertyChanges {

                            target: self

                            opacity: 0.6

                        }

                    }

                ]

            }

        }

...
}

When I grab the model item and change the property: model.get(x).selected = true

The visual item does not change, I have checked to see that the model item property it getting set correctly: console.log("item selected: " + model.get(x).selected)

Anyone have any luck with this, is it supposed to work?

Here's the model code:


    ListModel {

        id: filteritems

        ListElement { name: "people"; selected: true }

        ListElement { name: "time"; selected: false }

    }


My setup is a little more complex, as I am writing all my components in QML directly instead of any C++ (Crazy? maybe.) So there are a few levels of components going on, but everything else is working just fine.

Thanks for looking!
Alan

<ATT00001..txt>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20100722/ea0e49db/attachment.html 


More information about the Qt-qml mailing list