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. [SOLVED] How to get data from ListElement?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to get data from ListElement?

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 1.7k 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.
  • M Offline
    M Offline
    mivoligo
    wrote on last edited by
    #1

    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
    }@

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mivoligo
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Sure, pleasure. Happy coding :)

          (Z(:^

          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