[SOLVED] How to get data from ListElement?



  • My very first post here, so hello everyone!

    Now, about my problem.
    What I'm trying to achieve?
    I have some ListModel. When I click on an element in the list, the "popup" window appears with the text from that element I clicked.

    What I'm getting instead?

    If I use:
    @popuptext: listmodel1.get(index).name@
    I get:
    @ReferenceError: Can't find variable: index@

    I've tried with:
    @popuptext: listmodel1.get(1).name@

    and it shows text from the second element (as I expected).

    My question.
    How should I modify this code to work as I wanted?

    main.qml
    @// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1

    Rectangle {
    id: root
    width: 360
    height: 360

    ListView {
        id: list_view1
        x: 97
        y: 84
        width: 110
        height: 160
        delegate: Item {
            x: 5
            height: 40
            Row {
                id: row1
                spacing: 10
                Rectangle {
                    width: 40
                    height: 40
                    color: colorCode
                    MouseArea {
                        anchors.fill: parent
                        onClicked: root.state = "popupVisible"
                    }
                }
    
                Text {
                    text: name
                    anchors.verticalCenter: parent.verticalCenter
                }
    
            }
        }
        model: ListModel {
            id: listmodel1
            ListElement {
                name: "Grey"
                colorCode: "grey"
            }
    
            ListElement {
                name: "Red"
                colorCode: "red"
            }
    
            ListElement {
                name: "Blue"
                colorCode: "blue"
            }
    
            ListElement {
                name: "Green"
                colorCode: "green"
            }
        }
    }
    PopUp {
        id: popup
        anchors.centerIn: parent
        opacity: 0
        popuptext: listmodel1.get(index).name
    }
    states: [
        State {
            name: "popupVisible"
            PropertyChanges { target: popup; opacity:1 }
    
        }
    ]
    

    }@

    PopUp.qml
    @Rectangle {
    width: 200
    height: 200
    color: "lightblue"
    Text {
    id: text1
    text: qsTr("text")
    color: "white"
    anchors.centerIn: parent
    }
    property alias popuptext: text1.text
    }@


  • Moderators

    You can get the index with "ListView.currentIndex":http://qt-project.org/doc/qt-4.8/qml-listview.html#currentIndex-prop.



  • sierdzio, thanks for the answer! Helped me a lot.

    I've got it working.
    To onClicked I added:
    @list_view1.currentIndex = index@
    and I changed popuptext to:
    @popuptext: listmodel1.get(list_view1.currentIndex).name@

    Full code now:

    main.qml
    @// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1

    Rectangle {
    id: root
    width: 360
    height: 360

    ListView {
        id: list_view1
        x: 97
        y: 84
        width: 110
        height: 160
        delegate: Item {
            x: 5
            height: 40
            Row {
                id: row1
                spacing: 10
                Rectangle {
                    width: 40
                    height: 40
                    color: colorCode
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            list_view1.currentIndex = index;
                            root.state = "popupVisible"
                        }
                    }
                }
    
                Text {
                    text: name
                    anchors.verticalCenter: parent.verticalCenter
                }
    
            }
        }
        model: ListModel {
            id: listmodel1
            ListElement {
                name: "Grey"
                colorCode: "grey"
            }
    
            ListElement {
                name: "Red"
                colorCode: "red"
            }
    
            ListElement {
                name: "Blue"
                colorCode: "blue"
            }
    
            ListElement {
                name: "Green"
                colorCode: "green"
            }
        }
    }
    PopUp {
        id: popup
        anchors.centerIn: parent
        opacity: 0
        popuptext: listmodel1.get(list_view1.currentIndex).name
    }
    states: [
        State {
            name: "popupVisible"
            PropertyChanges { target: popup; opacity:1 }
    
        }
    ]
    

    }@

    PopUp.qml stays the same.


  • Moderators

    Sure, pleasure. Happy coding :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.