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. Scrolling inside text field
Forum Updated to NodeBB v4.3 + New Features

Scrolling inside text field

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 5 Posters 4.5k 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.
  • M Offline
    M Offline
    manny_lp
    wrote on last edited by
    #1

    Hi all,

    I am using QtQuick controls 2. How can we scroll (horizontally) inside the Text field, no. of characters is more than available width? I could not find any property that enables such requirement. I do not want to use arrow key to navigate through the characters entered. Instead I want to allow the user the scroll .

    Any solution?

    YunusY 1 Reply Last reply
    0
    • M Offline
      M Offline
      Mammamia
      wrote on last edited by
      #2

      @manny_lp You should create your own control for this like the one below

          Rectangle {
              width: parent.width
              height: 40
              border.color: "red"
      
              ScrollView {
                  id: view
                  anchors.fill: parent
                  clip: true
      
                  ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
                  ScrollBar.vertical.policy: ScrollBar.AlwaysOff
      
                  Label {
                      text: "This is a very long text which displayed on a Label with horizontal scrolling"
                      anchors.verticalCenter: parent.verticalCenter
                  }
              }
          }
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        manny_lp
        wrote on last edited by
        #3

        @Mammamia
        Thank you for answering.

        But in the solution you mentioned, text is not editable. I want the user to be able to input text using a virtual keyboard. When the text is longer than visible width of Text field, user should be able to scroll the move through the characters.

        M 1 Reply Last reply
        0
        • M manny_lp

          @Mammamia
          Thank you for answering.

          But in the solution you mentioned, text is not editable. I want the user to be able to input text using a virtual keyboard. When the text is longer than visible width of Text field, user should be able to scroll the move through the characters.

          M Offline
          M Offline
          Mammamia
          wrote on last edited by
          #4

          @manny_lp Sorry I missed that point.

          Then what you may need to do is to create a custom component that encapsulates a Label and TextField stacked. By default, you show the Label as shown above scrollable and when the user clicks on it handle it by a MouseArea and show the TextField with the same text form the Label.

          1 Reply Last reply
          0
          • M manny_lp

            Hi all,

            I am using QtQuick controls 2. How can we scroll (horizontally) inside the Text field, no. of characters is more than available width? I could not find any property that enables such requirement. I do not want to use arrow key to navigate through the characters entered. Instead I want to allow the user the scroll .

            Any solution?

            YunusY Offline
            YunusY Offline
            Yunus
            wrote on last edited by Yunus
            #5

            @manny_lp Actually I fixed your problem. All you need is changing current "CursorPosition". Here is the code:

            import QtQuick 2.9
            import QtQuick.Window 2.2
            import QtQuick.Controls 2.4
            import Qt.labs.platform 1.0
            Window {
                visible: true
                width: 640
                height: 480
                title: qsTr("Hello World")
                TextField {
                    id: control
                    x:0
                    y:0
                    width: 150
                    height: 50
                    placeholderText: qsTr("Enter description")
            
                    onCursorPositionChanged: console.log(control.cursorPosition)
            
                }
            
                Rectangle{
                    id:rect
                    x:0
                    y:50
                    width: 150
                    height: 50
                    color: "red"
                    MouseArea {
                        anchors.fill: parent
                        onWheel: {
                            if(wheel.angleDelta.y>0)
                            control.cursorPosition -= 1
                            if(wheel.angleDelta.y<0)
                            control.cursorPosition += 1
            
                        }
                    }
                }
            }
            
            

            The only problem is you need to scroll on the red area you can also assign the mouse area to ur textfield area

            ODБOïO 1 Reply Last reply
            0
            • YunusY Yunus

              @manny_lp Actually I fixed your problem. All you need is changing current "CursorPosition". Here is the code:

              import QtQuick 2.9
              import QtQuick.Window 2.2
              import QtQuick.Controls 2.4
              import Qt.labs.platform 1.0
              Window {
                  visible: true
                  width: 640
                  height: 480
                  title: qsTr("Hello World")
                  TextField {
                      id: control
                      x:0
                      y:0
                      width: 150
                      height: 50
                      placeholderText: qsTr("Enter description")
              
                      onCursorPositionChanged: console.log(control.cursorPosition)
              
                  }
              
                  Rectangle{
                      id:rect
                      x:0
                      y:50
                      width: 150
                      height: 50
                      color: "red"
                      MouseArea {
                          anchors.fill: parent
                          onWheel: {
                              if(wheel.angleDelta.y>0)
                              control.cursorPosition -= 1
                              if(wheel.angleDelta.y<0)
                              control.cursorPosition += 1
              
                          }
                      }
                  }
              }
              
              

              The only problem is you need to scroll on the red area you can also assign the mouse area to ur textfield area

              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by
              #6

              @Yunus said in Scrolling inside text field:

              you can also assign the mouse area to ur textfield area

              It will 'break' the TextFields own mouse handling
              : discussed here https://forum.qt.io/topic/64041/textfield-and-mousearea/10

              maybe possible workaround

              Window {
                  visible: true
                  width: 640
                  height: 480
              
                  TextField {
                      id: control
                      width: 150
                      height: 50
              
                      MouseArea { // handle TextField scroll and click to change cursor position
                          anchors.fill: parent
                          enabled: control.focus
                          onClicked: control.cursorPosition = control.positionAt(mouseX,mouseY)
                          onWheel: {
                              if(wheel.angleDelta.y>0)
                                  control.cursorPosition -= 1
                              if(wheel.angleDelta.y<0)
                                  control.cursorPosition += 1
                          }
                      }
                  }
              
                  Button{//will remove focus from control
                      text: 'ok'
                      anchors.centerIn: parent
                  }
              }
              
              YunusY 1 Reply Last reply
              1
              • ODБOïO ODБOï

                @Yunus said in Scrolling inside text field:

                you can also assign the mouse area to ur textfield area

                It will 'break' the TextFields own mouse handling
                : discussed here https://forum.qt.io/topic/64041/textfield-and-mousearea/10

                maybe possible workaround

                Window {
                    visible: true
                    width: 640
                    height: 480
                
                    TextField {
                        id: control
                        width: 150
                        height: 50
                
                        MouseArea { // handle TextField scroll and click to change cursor position
                            anchors.fill: parent
                            enabled: control.focus
                            onClicked: control.cursorPosition = control.positionAt(mouseX,mouseY)
                            onWheel: {
                                if(wheel.angleDelta.y>0)
                                    control.cursorPosition -= 1
                                if(wheel.angleDelta.y<0)
                                    control.cursorPosition += 1
                            }
                        }
                    }
                
                    Button{//will remove focus from control
                        text: 'ok'
                        anchors.centerIn: parent
                    }
                }
                
                YunusY Offline
                YunusY Offline
                Yunus
                wrote on last edited by
                #7

                @LeLev Ahh yes sorry, it is breaking. I didnt know that. Your solution seems better.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  manny_lp
                  wrote on last edited by
                  #8

                  @LeLev
                  I tried you soluttion, it works for scrolling using trackpad. But when we swipe/ scroll using touch (the device I am working on has touch screen, no mouse or external keyboard shall be used) it does not emit "wheel" event and scrolling does not happen!

                  ODБOïO 1 Reply Last reply
                  0
                  • M manny_lp

                    @LeLev
                    I tried you soluttion, it works for scrolling using trackpad. But when we swipe/ scroll using touch (the device I am working on has touch screen, no mouse or external keyboard shall be used) it does not emit "wheel" event and scrolling does not happen!

                    ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on last edited by
                    #9

                    @manny_lp hi, sry i only tested with mouse wheel

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      arkceajin
                      wrote on last edited by
                      #10

                      This can be implemented using ScrollView or Flickable. I create a demo in Github: https://github.com/arkceajin/QtDemos/tree/master/FlickableInput

                      I think it shall match the RS that you mentioned.
                      But it still needs some improvement for the text cursor which I will try to complete in future.

                      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