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 add transition to ListView : positionViewAtEnd()/positionViewAtBeginning()
QtWS25 Last Chance

How to add transition to ListView : positionViewAtEnd()/positionViewAtBeginning()

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 932 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.
  • F Offline
    F Offline
    Fheanor
    wrote on last edited by Fheanor
    #1

    Hello,

    I need some times to set my View of my ListView on lastItem or firstItem (positionViewAtEnd()/positionViewAtBeginning()).

    However, it is a brutal jump and I would like to add a Transition.

    I tried to add this :

        Behavior on contentY {
            id: contentYAnimation
            PropertyAnimation {duration: 300 }
        }
    

    But it doesn't change anything.

    Any suggestion ?

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

      @Fheanor Maybe you can try the same with ListView's currentIndex.

      157

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fheanor
        wrote on last edited by
        #3

        The best solution I found:

        Source

        import QtQuick 2.7
        import QtQuick.Controls 2.0
        
        ApplicationWindow
        {
            width: 1024
            height: 800
            visible: true
            property int lastAddedIndex: -1
            onLastAddedIndexChanged: gotoIndex(lastAddedIndex)
        
            function gotoIndex(idx) {
                anim.running = false;
        
                var pos = lv.contentX;
                var destPos;
        
                lv.positionViewAtIndex(idx, ListView.Contain);
                destPos = lv.contentX;
        
                anim.from = pos;
                anim.to = destPos;
                anim.running = true;
            }
        
            NumberAnimation { id: anim; target: lv; property: "contentX"; duration: 500 }
        
            Button {
                property int num: 10
                text: 'Add element: ' + num
                onClicked: {
                    lastAddedIndex = Math.floor(Math.random(num) * lm.count)
                    lm.insert(lastAddedIndex, { num : this.num })
                    num++
                }
            }
        
            ListModel {
                id: lm
                ListElement { num: 0 }
                ListElement { num: 1 }
                ListElement { num: 2 }
                ListElement { num: 3 }
                ListElement { num: 4 }
                ListElement { num: 5 }
                ListElement { num: 6 }
                ListElement { num: 7 }
                ListElement { num: 8 }
                ListElement { num: 9 }
            }
        
            ListView {
                id: lv
                model: lm
                y: 150
                width: 800
                height: 100
                spacing: 2
                clip: true
                orientation: ListView.Horizontal
                delegate: Rectangle {
                    width: 150
                    height: 100
                    border.color: 'black'
                    color: lastAddedIndex === index ? 'purple' : 'white'
                    Text {
                        anchors.centerIn: parent
                        text: index + ': ' + num
                    }
                }
        
                add: Transition { NumberAnimation { property: 'width'; from: 0; to: 150; duration: 600 } }
            }
        }
        
        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