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. Flickable snap behaviour
Qt 6.11 is out! See what's new in the release blog

Flickable snap behaviour

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 4 Posters 7.6k 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.
  • D Offline
    D Offline
    deimos
    wrote on last edited by
    #1

    Hi,

    I have a ListView that displays a full screen items. The default snap behaviour with "snapMode: ListView.SnapOneItem", is that when I drag for, lets say, 10 pixel, the flickable goes to the next item.
    What I would like to achieve is to start the snap after a drag of half the screen.
    Is this possible ?

    Thanks in advance

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cpscotti
      wrote on last edited by
      #2

      Ok, I have some Ideas that might help
      If you let go the automatic "snapMode: ListView.SnapOneItem", you can implement the behaviour yourself.
      I can think of something like this:
      @
      property double initContentY;
      property double singleItemHeight;//set this somewhere

      onMovementStarted: {
          //save contentY to a custom property on your flickable
          initContentY = contentY
      }
      
      onMovementEnded: {
         //
         var middleOfItem = (contentY-initContentY)%singleItemHeight
         if( Math.abs(middleOfItem) > (singleItemHeight/2)) {//more than half an item was flicked
              contentY += ( singleItemHeight - middleOfItem)
              //increment contentY to flick to the next item
         }
      }
      

      @
      I'm pretty sure you'd have to handle signs properly there even before the remainder operator but I think that's the right way to go...

      cpscotti.com/blog/ - Used when I need to kill some time at work :D

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deimos
        wrote on last edited by
        #3

        the logic sounds good for what I am trying to achieve.

        Thanks a lot, I will try this in a bit :)

        1 Reply Last reply
        0
        • J Offline
          J Offline
          juba
          wrote on last edited by
          #4

          Hi all,
          I have implemented a similar algorithm to calculate the current element when flicking:
          @ // Snap One Item policy
          property double initContentX;
          property double initElementNumber;
          property double singleItemWidth:;//set this somewhere

                  onMovementStarted:
                  {
                      //save contentX to a custom property on your flickable
                      initContentX = contentX
                      //also save current element number
                      initElementNumber = contentX / singleItemWidth
                  }
          
                  onMovementEnded:
                  {
                      var numberOfItem = Math.round((contentX - initContentX) / singleItemWidth);
                      contentX = (singleItemWidth * numberOfItem)
                              + (initElementNumber * singleItemWidth)
                  }@
          

          Now my problem is how to set a transition in order to not move immediately to the contentX...
          I try with a simple transition on contentX defined inside my Flickable, but it doesn't work...

          Any suggestion?

          Thanks in advance.

          C 1 Reply Last reply
          0
          • J juba

            Hi all,
            I have implemented a similar algorithm to calculate the current element when flicking:
            @ // Snap One Item policy
            property double initContentX;
            property double initElementNumber;
            property double singleItemWidth:;//set this somewhere

                    onMovementStarted:
                    {
                        //save contentX to a custom property on your flickable
                        initContentX = contentX
                        //also save current element number
                        initElementNumber = contentX / singleItemWidth
                    }
            
                    onMovementEnded:
                    {
                        var numberOfItem = Math.round((contentX - initContentX) / singleItemWidth);
                        contentX = (singleItemWidth * numberOfItem)
                                + (initElementNumber * singleItemWidth)
                    }@
            

            Now my problem is how to set a transition in order to not move immediately to the contentX...
            I try with a simple transition on contentX defined inside my Flickable, but it doesn't work...

            Any suggestion?

            Thanks in advance.

            C Offline
            C Offline
            CoolPraveena
            wrote on last edited by
            #5

            @juba
            I tried the below code, it is working for me;

            flickAreaX = (singleItemWidth * numberOfItem)
            + (initElementNumber * singleItemWidth)
            SequentialAnimation {
            id: animation
            NumberAnimation {
            target: flickArea
            property: "contentX"
            to: flickAreaX
            duration: 200
            easing.type: Easing.InOutQuad
            }
            }

            Thanks !!

            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