<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to control scrolling in TextArea]]></title><description><![CDATA[<p dir="auto">Hi<br />
I want to use TextArea to display colored logs. I know that ListView is more efficient for this task, but for my use case TextArea fits (and I like the colored borders when focused). I wrapped the TextArea in a ScrollView, as the documentation suggests. I color and add text through a C++ class that implements QSyntaxHighlighting and inserts text through QTextCursor. So I don't use the TextArea append function or the +=. The problem is that every time I add new text, ScrollView scrolls to the end automatically, but I want to scroll to the top and look at the text that was added earlier, and when I'm done, I want to enable auto-scrolling again by just scrolling to the end or perhaps by using a button. I don't see a clear way to achieve this behavior. Many thanks in advance.</p>
<p dir="auto">the qml file code is</p>
<pre><code>ScrollView {
  id: textAreaView
  Layout.fillHeight: true
  Layout.fillWidth: true
  wheelEnabled: true
  background: Rectangle {
    color: Material.background
    border.color: textAreaView.focus ? Material.accent : "#bdbebf"
    border.width: 2
  }

  TextArea {
    id: textArea
    height: _root.height - textInput.height
    readOnly: true
    textFormat: TextEdit.PlainText
    width: _root.width
    wrapMode: Text.Wrap

    Component.onCompleted: {
       highlighter.onCompleted()
    }
                
    Connections {
      target: logger
      onDeviceLogReceived: function (msg) { 
         highlighter.append(msg);
       }
     }
                
    HighlighterComponent {
       id: highlighter
    }
}
</code></pre>
<p dir="auto">the c++ highlighter function</p>
<pre><code>void Highlighter::append(const QString&amp; text) {
    QTextCursor cursor(doc);
    cursor.movePosition(QTextCursor::End);

    static QRegularExpression regex("^\[\\d+:\\d+:\\d+\\.\\d+,\\d+\] &lt;(dbg|inf|wrn|err)&gt;");
    QRegularExpressionMatch match = regex.match(text);

    if (match.hasMatch()) {
        QString tag = match.captured(1);
        MessageType type = getMessageType(tag);
        QTextCharFormat format = highlightingRules.value(type);

        cursor.insertText(match.captured(0), format);
        cursor.insertText(text.mid(match.capturedEnd()), defaultFormat);
    } else {
        cursor.insertText(text, defaultFormat);
    }
    cursor.insertText("\n", defaultFormat);
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/145393/how-to-control-scrolling-in-textarea</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 23:53:08 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/145393.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 26 May 2023 16:15:12 GMT</pubDate><ttl>60</ttl></channel></rss>