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. How to remember the previous selected item in this example
QtWS25 Last Chance

How to remember the previous selected item in this example

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 4 Posters 4.6k 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.
  • R Offline
    R Offline
    raghava.chinnappa
    wrote on last edited by
    #1

    This is the simple ListView Example code. Here I need to achieve Highlight the selected/clicked item and de-select the previous highlighted item.
    Can someone help me achieve this. I couldn’t find the way to remember the previously selected item. Will the Javascript help to achieve this?

    Thanks in advance
    ~ Raghava

    @import Qt 4.7
    Rectangle {
    width: 360
    height: 640
    ListModel{
    id: lisModelExample
    ListElement{ name: 'Apple'}
    ListElement{ name: 'Ball'}
    ListElement{ name: 'Cat'}
    ListElement{ name: 'Doll'}
    }
    ListView{
    id: listViewExample
    width: 360
    height: 640
    model: lisModelExample
    delegate: delegateExample
    }
    Component{
    id: delegateExample
    Rectangle{
    id: listItem
    width: 360
    height: 40
    MouseArea {
    anchors.fill: parent
    onClicked: listItem.state == 'highlight' ? listItem.state = "" : listItem.state = 'highlight';
    }
    states: State {
    name: "highlight"
    PropertyChanges {
    target: listItem
    color: "#fbf70a"
    }
    }
    Text{
    x: 10
    y: 10
    text: model.name
    }

         }
     }
    

    }@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Why are you creating the highlight like that?

      The QML view elements have a build in capability of creating a highlight for the current item. What's more, you are explicitly warned not to store state in a delegate. That is exactly what you are doing.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raghava.chinnappa
        wrote on last edited by
        #3

        Hi Andre,

        Do you any link where I can read more about these built-in capabilities in view elements. I couldn't able to find one on documentation.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Well, the "QML ListView Element documentation":http://doc.qt.nokia.com/4.7/qml-listview.html would be a good start. Especially the documentation on members that start with the word "highlight" would be relevant, I'd say.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            raghava.chinnappa
            wrote on last edited by
            #5

            Thanks for your input. It helps my requirements.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kxyu
              wrote on last edited by
              #6

              i don't like built-in highlight. You can do this:

              @import Qt 4.7
              Rectangle {
              width: 360
              height: 640
              ListModel{
              id: lisModelExample
              ListElement{ name: 'Apple'}
              ListElement{ name: 'Ball'}
              ListElement{ name: 'Cat'}
              ListElement{ name: 'Doll'}
              }
              ListView{
              id: listViewExample
              width: 360
              height: 640
              model: lisModelExample
              delegate: delegateExample
              }
              Component{
              id: delegateExample
              Rectangle{
              id: listItem
              width: 360
              height: 40
              MouseArea {
              anchors.fill: parent
              onClicked: listViewExample.currentIndex=index
              }
              states: State {
              name: "highlight"
              when:index==listViewExample.currentIndex
              PropertyChanges {
              target: listItem
              color: "#fbf70a"
              }
              }
              Text{
              x: 10
              y: 10
              text: model.name
              }

                   }
               }
              

              }@

              1 Reply Last reply
              0
              • R Offline
                R Offline
                raghava.chinnappa
                wrote on last edited by
                #7

                Kxyu, yeah you're code worked quite well. Thanks for that

                Andre, I'm wondering why even after using the "hightlight" property in the ListView didn't work out for me. I just used the sample code and followed the steps as mentioned in the documentation.

                @import Qt 4.7
                Rectangle {
                id: body
                width: 360
                height: 640

                 ListModel{
                     id: lisModelExample
                     ListElement{ name: 'Apple'}
                     ListElement{ name: 'Ball'}
                     ListElement{ name: 'Cat'}
                     ListElement{ name: 'Doll'}
                 }
                
                 ListView{
                     id: listViewExample
                     width:  360
                     height:  640
                     model: lisModelExample
                     delegate: delegateExample
                     highlight: highLightItem
                     focus: true
                 }
                
                 Component{
                     id: delegateExample
                     Text{
                         id: textLabel
                         x: 10
                         y: 10
                         text:  model.name
                     }
                 }
                
                 Component {
                     id: highLightItem
                     Rectangle {
                         width: 180; height: 20
                         color: "lightsteelblue"; radius: 5
                         y: listViewExample.currentItem.y
                         Behavior on y {
                             SpringAnimation {
                                 spring: 3
                                 damping: 0.2
                             }
                         }
                     }
                 }
                

                }@

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  raghava.chinnappa
                  wrote on last edited by
                  #8

                  Hmm.. Interesting. If I navigate using Keyboard, the Highlight is working fine. But not with the mouse click on the item.

                  Added MouseArea in the delegate-item to make it work. Is that right way to do?

                  @ Component{
                  id: delegateExample
                  Text{
                  id: textLabel
                  x: 10
                  y: 10
                  text: model.name

                           MouseArea{
                               anchors.fill : parent
                               onClicked: {
                                   listViewExample.currentIndex = index;
                               }
                           }
                       }
                   }
                  

                  @

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mbrasser
                    wrote on last edited by
                    #9

                    [quote author="raghava.chinnappa" date="1294696961"]Added MouseArea in the delegate-item to make it work. Is that right way to do?
                    [/quote]

                    Yes, that's correct -- the QML views do not automatically set the currentIndex/currentItem by mouse; it is up to the developer to decide how they want to handle that (unfortunately, this doesn't seem to be well documented at the moment; we'll add additional docs for this moving forward).

                    Regards,
                    Michael

                    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