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. Make an added ListViewItem the currentItem
Qt 6.11 is out! See what's new in the release blog

Make an added ListViewItem the currentItem

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 4 Posters 979 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.
  • S Offline
    S Offline
    Suli Sahne
    wrote on last edited by
    #1

    Hello guys,

    does anyone have an idea how I can easily turn a newly added item to a currentItem in a ListView?

    J.HilkJ 1 Reply Last reply
    0
    • S Suli Sahne

      Hello guys,

      does anyone have an idea how I can easily turn a newly added item to a currentItem in a ListView?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Suli-Sahne

      without testing it:

      myView.currentItem = myView.itemAtIndex(myView.model.count -1)
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Suli Sahne
        wrote on last edited by
        #3

        Thanks, but the new item is not necessarily at the end..

        I would need something like that:

        ListView {
          onAdded: {
            currentIndex = addedIndex
          }
        }
        
        1 Reply Last reply
        0
        • F Offline
          F Offline
          flowery
          wrote on last edited by
          #4

          So , you want to handle both insert and add operation .Use ListView.onAdd{console.log("added index "+index)} handler inside delegate .

          S 1 Reply Last reply
          0
          • S Offline
            S Offline
            Suli Sahne
            wrote on last edited by
            #5

            I tried to use a ScriptAction inside the add Transition:

            ListView {
              id: myList
              add: Transition {
                ScriptAction {
                  script: {
                    console.log("add", ViewTransition.index)
                    myList.currentIndex = ViewTransition.index
                  }
                }
              }
            }
            

            But probably for performance reasons the Transition is called only if the new item is visible..

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Suli Sahne
              wrote on last edited by
              #6

              The next attempt was to react to the rowInserted signal of the QAIM.

              ListView {
                id: myList
                model: myModel
              }
              
              Connections {
               target: myModel
                function onRowsInserted(parent, first, last) {
                  console.log("inserted", first, last)
                }
              }
              

              but the signal is triggered before the list is updated..

              1 Reply Last reply
              0
              • F flowery

                So , you want to handle both insert and add operation .Use ListView.onAdd{console.log("added index "+index)} handler inside delegate .

                S Offline
                S Offline
                Suli Sahne
                wrote on last edited by
                #7

                @flowery ahhh ok, I got it. I have never heard of this attached property before. It works, thanks a lot!

                ListView {
                  id: myList
                  delegate: ItemDelegate {
                    ListView.onAdd: {
                      myList.currentIndex = index
                    }
                  }
                }
                
                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Suli Sahne
                  wrote on last edited by
                  #8

                  too early... ListView.onAdd is only called when the new item is visible.

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

                    use Qt.callLater in your rowsInserted signal handler.

                    S 1 Reply Last reply
                    0
                    • GrecKoG GrecKo

                      use Qt.callLater in your rowsInserted signal handler.

                      S Offline
                      S Offline
                      Suli Sahne
                      wrote on last edited by
                      #10

                      @GrecKo perfect, that finally worked. Thanks a lot.

                      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