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. ListView, PositionViewAtBeginning()
Qt 6.11 is out! See what's new in the release blog

ListView, PositionViewAtBeginning()

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

    Hi!

    I have a listview. When I click on an item, I clear the model and add new items in the model (from c++).
    This works fine, and the listview is updated properly.

    The only problem is that the position of the listview is not changed when the model is updated. For example, if I scroll down to the end of the list, click on the last item, then, the list would be updated with new items, but I would still see the last item.
    I would like to set the position to the beginning of the list when an item is clicked, but I don't know how to do that...

    I tried to call positionViewAtBeginning() and to set currentIndex to 0 with no luck. Any help?

    Thanks!

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      As per the docs
      bq. Note: methods should only be called after the Component has completed. To position the view at startup, this method should be called by Component.onCompleted, viz :
      @
      Component.onCompleted: positionViewAtBeginning()
      @

      So are you calling it in the above mentioned way ?

      157

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JF002
        wrote on last edited by
        #3

        Yes, I tried to add this line, but it seems that Component.onCompleted is called only once, when the component is created at launch time.

        But when I remove/add data from the model, the list is updated accordingly, but onComleted is not called.

        So, how to change the position of the list after the component has been created?

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Hi,

          Depending upon your use case i created a small code, it positions the item to the top after clearing the list.
          May be this would be useful

          @

          import QtQuick 2.0

          Rectangle {
          width: 320; height: 240

          ListModel {
              id: model
              ListElement {
                  name: "PrePopulated"
                  number: "32423434"
              }
              ListElement {
                  name: "PrePopulated"
                  number: "23423232"
              }
              ListElement {
                  name: "PrePopulated"
                  number: "23323234"
              }
          }
          
          Component {
              id: delegate
              Item {
                  width: 320; height: 40
                  Column {
                      Text { text: '<b>Name:</b> ' + name }
                      Text { text: '<b>Number:</b> ' + number }
                  }
          
                  MouseArea {
                      anchors.fill: parent
                      onClicked: {
                          //If clicked on last item in list, clear the list and populate the new items
                          if(index===view.count-1)
                          {
                              console.log(index);
                              addItem();
                          }
                      }
                  }
              }
          }
          
          ListView {
              id: view
              anchors.fill: parent
              model: model
              delegate: delegate
              highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
              focus: true
          }
          
          //Click to add Items to listview
          Text {
              id: add
              text: "Add Item"
              anchors.bottom: parent.bottom
              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      model.append({name: "ButtonClick", number: Math.floor((Math.random()*10000)+1).toString()} );
                      view.positionViewAtEnd();
                  }
              }
          }
          
          function addItem()
          {
              model.clear();
              for(var i=0; i<10; i++)
              {
                  var data = {'name': "ItemClicked " + i.toString(), 'number': Math.floor((Math.random()*10000)+1).toString() };
                  model.append(data);
                  view.positionViewAtBeginning();
              }
          }
          

          }

          @

          157

          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