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 retrieve the position in a ListView from a ListView

How retrieve the position in a ListView from a ListView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 3 Posters 451 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.
  • C Offline
    C Offline
    cosmoff
    wrote on last edited by
    #1

    Hello everybody,

    I make a ListView in a listView like :

    ListView{
        id: id_a
        orientation: ListView.Vertical
        verticalLayoutDirection: ListView.TopToBottom
        model : a
    
        delegate : Item{
    
            ListView{
                id : id_b
                orientation: ListView.Horizontal
                layoutDirection: ListView.RightToLeft
    
                model : b
    
                delegate : Rectangle{
                    width: 10
                    height: 10
                     MouseArea{
                         anchors.fill:parent
                         onClicked: {
                             console.log("y : " + A.length + "  x : " + B.length )
                             console.log("y : " + id_a.currentItem + "  x : " + id_b.currentItem )
                             console.log("y : " + id_a.count + "  x : " + id_b.count )
                         }
                     }
                }
            }
        }
    
    }
    

    I show you only the information important in my example. As you can see I would like to know the position where I clicked, but the console return always the same thing except for the currentItem which change:
    qml: y : 150 x : 320
    qml: y : QQuickItem(0x558c5a7f64c0) x : QQuickRectangle(0x558c5c9b1220)
    qml: y : 150 x : 320

    do you have an idea to how can I retrieve the position where I click ?
    thanks

    sierdzioS 1 Reply Last reply
    0
    • C cosmoff

      Hello everybody,

      I make a ListView in a listView like :

      ListView{
          id: id_a
          orientation: ListView.Vertical
          verticalLayoutDirection: ListView.TopToBottom
          model : a
      
          delegate : Item{
      
              ListView{
                  id : id_b
                  orientation: ListView.Horizontal
                  layoutDirection: ListView.RightToLeft
      
                  model : b
      
                  delegate : Rectangle{
                      width: 10
                      height: 10
                       MouseArea{
                           anchors.fill:parent
                           onClicked: {
                               console.log("y : " + A.length + "  x : " + B.length )
                               console.log("y : " + id_a.currentItem + "  x : " + id_b.currentItem )
                               console.log("y : " + id_a.count + "  x : " + id_b.count )
                           }
                       }
                  }
              }
          }
      
      }
      

      I show you only the information important in my example. As you can see I would like to know the position where I clicked, but the console return always the same thing except for the currentItem which change:
      qml: y : 150 x : 320
      qml: y : QQuickItem(0x558c5a7f64c0) x : QQuickRectangle(0x558c5c9b1220)
      qml: y : 150 x : 320

      do you have an idea to how can I retrieve the position where I click ?
      thanks

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by sierdzio
      #2

      @cosmoff said in How retrieve the position in a ListView from a ListView:

      do you have an idea to how can I retrieve the position where I click ?

      onClicked: {
        console.log("y : " + mouse.y + "  x : " + mouse.x )
      }
      

      (Z(:^

      C 1 Reply Last reply
      0
      • sierdzioS sierdzio

        @cosmoff said in How retrieve the position in a ListView from a ListView:

        do you have an idea to how can I retrieve the position where I click ?

        onClicked: {
          console.log("y : " + mouse.y + "  x : " + mouse.x )
        }
        
        C Offline
        C Offline
        cosmoff
        wrote on last edited by
        #3

        @sierdzio In fact I want to know in what Item I click

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cosmoff
          wrote on last edited by
          #4

          for example, I want to know on what line I clicked on the first ListView, and on what colomn I clicked on the second ListView

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

            You can use itemAt() function for that purpose. For your parent item you'd have to map the coordinates.

            (Z(:^

            1 Reply Last reply
            2
            • C Offline
              C Offline
              cosmoff
              wrote on last edited by
              #6

              thanks for your messages. I did :

              var item = b.itemAt(mouse.x, mouse.y); to retrieve the column that I need but it returns item = null
              

              There is no example on Qt, do you have an example ?

              1 Reply Last reply
              0
              • GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #7

                You can access the index of a delegate using the index context property (or model.index) :

                ListView{
                    id: id_a
                    orientation: ListView.Vertical
                    verticalLayoutDirection: ListView.TopToBottom
                    model : a
                
                    delegate : Item{
                
                        ListView{
                            id : id_b
                            readonly property int rowIndex: index
                            orientation: ListView.Horizontal
                            layoutDirection: ListView.RightToLeft
                
                            model : b
                
                            delegate : Rectangle{
                                width: 10
                                height: 10
                                 MouseArea{
                                     anchors.fill:parent
                                     onClicked: {
                                         console.log("row", id_b.rowIndex, "column", index)
                                     }
                                 }
                            }
                        }
                    }
                }
                
                C 1 Reply Last reply
                1
                • GrecKoG GrecKo

                  You can access the index of a delegate using the index context property (or model.index) :

                  ListView{
                      id: id_a
                      orientation: ListView.Vertical
                      verticalLayoutDirection: ListView.TopToBottom
                      model : a
                  
                      delegate : Item{
                  
                          ListView{
                              id : id_b
                              readonly property int rowIndex: index
                              orientation: ListView.Horizontal
                              layoutDirection: ListView.RightToLeft
                  
                              model : b
                  
                              delegate : Rectangle{
                                  width: 10
                                  height: 10
                                   MouseArea{
                                       anchors.fill:parent
                                       onClicked: {
                                           console.log("row", id_b.rowIndex, "column", index)
                                       }
                                   }
                              }
                          }
                      }
                  }
                  
                  C Offline
                  C Offline
                  cosmoff
                  wrote on last edited by
                  #8

                  @GrecKo yes It works for the column but the console returns row indefined

                  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