TextArea won't scroll up when new text added.
Solved
QML and Qt Quick
-
I have the following implementation of a simple console type window for scrolling text output:
Rectangle { id: consoleOutput_rectangle x: 40 y: 112 width: 720 height: 400 color: "#ffffff" Flickable { id: flickable anchors.fill: parent contentWidth: consoleOutput_textArea.width contentHeight: consoleOutput_textArea.height flickableDirection: Flickable.VerticalFlick clip: true TextArea.flickable: TextArea { id: consoleOutput_textArea objectName: "ConsoleTextObject" anchors.fill: parent text: qsTr("Initial Text Area Text") wrapMode: Text.WordWrap onTextChanged: { console.log("OnTextChanged - height: " + consoleOutput_textArea.height + " " + flickable.height) if (consoleOutput_textArea.height > flickable.height) { flickable.contentY = consoleOutput_textArea.height - flickable.height console.log("OnTextChanged - contentY changed to: " + flickable.contentY) } } function appendText(additionalText) { text += additionalText } } ScrollBar.vertical: ScrollBar {} } }
If I use this to add text at the bottom from C++ code, using the append function of TextArea:
QMetaObject::invokeMethod(mConsoleTextObject, "append", Q_ARG(QString, additionalText));
it works except that append is line oriented, meaning it inserts a line feed after every line. So I added the appendText function seen in the QML above and invoke it like this:
QMetaObject::invokeMethod(mConsoleTextObject, "appendText", Q_ARG(QVariant, additionalText));
It works, except that now the text does not scroll up when it reaches the bottom. The console.log statements indicate that the onTextChanged functions is being called and the contentY number seems to be correct.
Am I missing some kind of repaint/update that's built into the native append? (I tried to call flickable.update() but it complained that the is no content in the flickable.)