I have a QTextBrowser widget and I'm adding text to this widget like
QTextBrowser m_outputLog;
...
void MainWindow::readStdout()
{
if (m_running)
{
QByteArray data = m_runProcess->readAllStandardOutput();
QString text = QString::fromUtf8(data);
if (!text.isEmpty())
{
m_outputLog->moveCursor (QTextCursor::End);
m_outputLog->insertPlainText (text);
m_outputLog->moveCursor (QTextCursor::End);
}
}
}
the readStdout is connected to the m_outputLog through the signal / slot mechanism.
This all works OK.
The text is appended at the end though the disadvantage is that with each insertion there is a jump back to the end even when I did scroll up a bit.
When I remove the m_outputLog->moveCursor (QTextCursor::End); statements the text is still nicely appended at the end but there is no automatic show of the text, I always have to use the mouse to scroll down.
- how can I put the text at the end
- automatically show the new text when the end of the text was already shown
- stay at the scroll position when I used the mouse to scroll through the text
Any suggestions?
Here is what I have done for my own need:
Basically, you need to save the scrollbar position before adding the new text, and then decide to move back the scrollbar to this position depending on your own criteria (for example, I decided not to move the scrollbar only if the user has selected some text).