QML TextArea cursorPosition after adding richtext
-
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 settextArea.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 -
@Diego-Donate have you tried calling forceActiveFocus() first before setting cursorPosition?
-
@Diego-Donate said in QML TextArea cursorPosition after adding richtext:
import QtQuick 2.7
import QtQuick.Controls 1.3Item {
height: 600
width: 600
anchors.centerIn: parentTextArea { 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
-
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