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. QML TextArea cursorPosition after adding richtext
QtWS25 Last Chance

QML TextArea cursorPosition after adding richtext

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 2.4k 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.
  • Diego DonateD Offline
    Diego DonateD Offline
    Diego Donate
    wrote on last edited by
    #1

    Hi,

    I have a QML TextArea (RichText), where I append an image from resources (using HTML <img>)
    After appending, the cursor goes automatically to 0, although I set

    textArea.cursorPosition = textArea.text.length
    

    How can I keep the cursor at the end, to keep writing?

    My code:

    import QtQuick 2.7
    import QtQuick.Controls 1.3
    
    Item {
        height: 600
        width: 600
        anchors.centerIn: parent
    
        TextArea {
            id: textArea
            anchors.centerIn: parent
            height: 300
            width: 500
    
            textFormat: TextEdit.RichText // diego.donate
            verticalAlignment: TextEdit.AlignVCenter
            font. pointSize: 14
            horizontalAlignment: Text.AlignLeft
        }
    
        DropArea {
            anchors.fill: textArea
    
            onEntered: drag.accepted = true
            onDropped: {
                var sImageFromRes = "qrc:///images/image.png"
                textArea.text += "<img src=" + sImageFromRes + " height=25 width=25>"
                textArea.cursorPosition = textArea.text.length // NOT working
            }
        }
    }
    

    Thanks,
    Diego

    L 1 Reply Last reply
    0
    • Diego DonateD Diego Donate

      Hi,

      I have a QML TextArea (RichText), where I append an image from resources (using HTML <img>)
      After appending, the cursor goes automatically to 0, although I set

      textArea.cursorPosition = textArea.text.length
      

      How can I keep the cursor at the end, to keep writing?

      My code:

      import QtQuick 2.7
      import QtQuick.Controls 1.3
      
      Item {
          height: 600
          width: 600
          anchors.centerIn: parent
      
          TextArea {
              id: textArea
              anchors.centerIn: parent
              height: 300
              width: 500
      
              textFormat: TextEdit.RichText // diego.donate
              verticalAlignment: TextEdit.AlignVCenter
              font. pointSize: 14
              horizontalAlignment: Text.AlignLeft
          }
      
          DropArea {
              anchors.fill: textArea
      
              onEntered: drag.accepted = true
              onDropped: {
                  var sImageFromRes = "qrc:///images/image.png"
                  textArea.text += "<img src=" + sImageFromRes + " height=25 width=25>"
                  textArea.cursorPosition = textArea.text.length // NOT working
              }
          }
      }
      

      Thanks,
      Diego

      L Offline
      L Offline
      literA2
      wrote on last edited by
      #2

      @Diego-Donate have you tried calling forceActiveFocus() first before setting cursorPosition?

      1 Reply Last reply
      0
      • Diego DonateD Offline
        Diego DonateD Offline
        Diego Donate
        wrote on last edited by
        #3

        @Diego-Donate said in QML TextArea cursorPosition after adding richtext:

        import QtQuick 2.7
        import QtQuick.Controls 1.3

        Item {
        height: 600
        width: 600
        anchors.centerIn: parent

        TextArea {
            id: textArea
            anchors.centerIn: parent
            height: 300
            width: 500
        
            textFormat: TextEdit.RichText // diego.donate
            verticalAlignment: TextEdit.AlignVCenter
            font. pointSize: 14
            horizontalAlignment: Text.AlignLeft
        }
        
        DropArea {
            anchors.fill: textArea
        
            onEntered: drag.accepted = true
            onDropped: {
                var sImageFromRes = "qrc:///images/image.png"
                textArea.text += "<img src=" + sImageFromRes + " height=25 width=25>"
                textArea.cursorPosition = textArea.text.length // NOT working
            }
        }
        

        }

        The trick is to use textArea.length instead of textArea.text.length

        1 Reply Last reply
        0
        • P Offline
          P Offline
          patrik08
          wrote on last edited by patrik08
          #4

          Hi ... you can use int "cursorPosition() after drag or insert ... setCursorPosition(int position); from QML2 DocumentHandler "sample from qt team..
          it work ... i tested ...
          On QML you not loast function versus QtTextarea or edit as QWidget ... i try to port my app https://sourceforge.net/projects/oasidoc/ on Iphone6 all in QtQuick .. so i can hear pdf & other document .. like a AudioBook from text...

          here QML2 DocumentHandler
          https://github.com/pehohlva/QOASIS/blob/master/test/texteditor/documenthandler.h
          or in your source qt5:
          /qt5.9.1/examples/quickcontrols2/texteditor

          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