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. Positioning a ScrollView to a particluar item
Forum Updated to NodeBB v4.3 + New Features

Positioning a ScrollView to a particluar item

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

    How do you go about setting a ScrollView to the location of a particular item attached to it in Qt 6.5.x?
    I've got a long user-interface in multiple QML files that I attach to a ScrollView. I want to be able to automagically scroll to a TextArea when the user clicks in it so that it snaps to the top of the view so that it doesn't get blocked by the virtual keyboard on a phone.

    All of the Google references on the subject make use of things in Qt 5 that are no longer accessible under Qt 6.5.x e.g. the Flickable. Yes, I can get at an attached scroll bar but that has a range of 0.0 to 1.0-size so I'd have to come up with a way of figuring out where in that range the TextArea would be.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      RogueWarrior
      wrote on last edited by
      #2

      So, one way to do this is to set contentItem to a Flickable. Then you can set contentY to the y position of the item you want. But that isn't easy to get especially if you have several embedded qml files with an arbitrary number of totally different user-interface elements in there. How one iterates through a list of child Items to get to the one you're want is a mystery.

      1 Reply Last reply
      0
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #3

        What have you tried? An example would be nice to work from.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          This works for me with Qt 6.5.6:

          import QtQuick
          import QtQuick.Controls
          
          Window {
              width: 640
              height: 480
              visible: true
          
              property Item targetItem: sv.contentChildren[0].children[25]
              property real targetPosition: targetItem.y / sv.contentHeight
          
              ScrollView {
                  id: sv
                  Column {
                      Repeater {
                          model: 100
                          delegate: Text {
                              text: index
                          }
                      }
                  }
              }
          
              Button {
                  id: button
                  anchors.right: parent.right
                  text: "scroll to " + targetItem.text
                  onClicked: sv.ScrollBar.vertical.position = targetPosition
              }
          }
          

          Asking a question about code? http://eel.is/iso-c++/testcase/

          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