[Qt-qml] How to access item in ListView delegate?

martin.jones at nokia.com martin.jones at nokia.com
Fri Sep 9 11:24:16 CEST 2011


Hi Shawn,

You do not need to add an index role to your model.  This property is available automatically in delegates.
http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodels.html

Br,
Martin.
________________________________________
From: qt-qml-bounces+martin.jones=nokia.com at qt.nokia.com [qt-qml-bounces+martin.jones=nokia.com at qt.nokia.com] On Behalf Of ext Shawn [shawn at forevertrusts.com]
Sent: Friday, 9 September 2011 7:06 PM
To: Riaan Kruger; Qt-qml at qt.nokia.com
Subject: Re: [Qt-qml] How to access item in ListView delegate?

Hi Riaan,

Thank you for your suggestion, I add index attribute to the model, then
it will work.
If model is from C++, I think the theory should be the same.
Rectangle {
    width: 180; height: 200
    ListModel {
        id: model
        ListElement {
            name: "Bill Smith"
            number: "555 3264"
            index: 0
        }
        ListElement {
            name: "John Brown"
            number: "555 8426"
            index: 1
        }
        ListElement {
            name: "Sam Wise"
            number: "555 0473"
            index: 2
        }
    }

    Component {
        id: contactDelegate
        Item {
            width: 180; height: 40
            scale: (view.currentIndex == index) ? 1.2 : 1
            Column {
                Text { text: '<b>Name:</b> ' + name }
                Text { text: '<b>Number:</b> ' + number }
            }
        }
    }

    ListView {
        id: view
        anchors.fill: parent
        model: model
        delegate: contactDelegate
        highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
        focus: true
    }
}


Best Regards,
Shawn

On Thu, 2011-09-08 at 12:37 +0200, Riaan Kruger wrote:
> Are you trying to scale the current item in testView?
> If so you should probably do it in the delegate with something like:
> Image {
>                id: image
>                width: 30
>                height: 30
>                source: model.contactImage
>                scale: parent.index == testview.currentIndex ? 2 :1
>            }
>
> PS:Have not tested the above
>
>
> On Thu, Sep 8, 2011 at 12:18 PM, Shawn <shawn at forevertrusts.com> wrote:
> > Hi guys,
> >
> > I want to access item in ListView delegate, like below code snippet,
> > I want to change image's property, which is in the delegate of testView.
> > It doesn't work. How can I access it or am I in the wrong way?
> > Any suggestion could be helpful.
> > Thank you very much!
> > ListView {
> >    id: testView
> >    model: easyNameModel
> >    delegate: testViewDelegate
> >    highlight: Rectangle {color: "green"; radius: 5;
> >        width: listview2.width; height: itemHeight
> >    }
> >
> >    Keys.onPressed: {
> >        if(event.key == Qt.Key_Down)
> >        {
> >            //image.scale = 2; // This doesn't work
> >            listview2.incrementCurrentIndex();
> >        }
> >
> >    }
> >
> >    Component{
> >        id:testViewDelegate
> >        Item{
> >            width: 100
> >            height: 30
> >            Image {
> >                id: image
> >                width: 30
> >                height: 30
> >                source: model.contactImage
> >            }
> >        }
> >    }
> > }
> >
> >
> > Best Regards,
> > Shawn
> >
> > _______________________________________________
> > Qt-qml mailing list
> > Qt-qml at qt.nokia.com
> > http://lists.qt.nokia.com/mailman/listinfo/qt-qml
> >


_______________________________________________
Qt-qml mailing list
Qt-qml at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-qml


More information about the Qt-qml mailing list