0

I have a QPlainTextEdit,which I have to populate in two ways.

When I want to add text at the end , I can do that very simply by using appendPlainText() function provided. I do this when Vertical scrollbar hits lower boundary and if user scrolls after that , then I append new text. This performs very smoothly.

But What I want to do is when user scrolls up and scroll bar hits the upper boundary, and if user scrolls after that(in upward direction), I want to prepend text to it. But the problem is there is no such function prependPlainText() and therefore, I first get plaintext from my QPlainTextEdit, which is a QString, use prepend of QString, and then append new text to the QPlainTextEdit. But problem is scroll bar goes down right after I append text to my QPlainTextEdit, What I want is keep the scroll bar at upper boundary. Just like scroll bar remains at lower boundary in previous scenario.

Sumeet
  • 8,086
  • 3
  • 25
  • 45

2 Answers2

0

Combining this and that id go with

ui->qpte->document()->setPlainText(text + "\n" + ui->qpte->toPlainText());

with qpte being the name of the QPlainTextEdit and text a QString with the new text. ui is a pointer to your UI::MainWindow or QWidget or so.

Not sure if this is performance wise (and certainly doesnt scale well)

x29a
  • 1,761
  • 1
  • 24
  • 43
0

If you are going to use x29a's approach you might consider using it like this:

ui->qpte->document()->setPlainText(text + "\n" + ui->qpte->toHtml());

so that your previous formatting does get discarded.

Or maybe it's better to use what is suggested here like this:

QTextCursor cursor = QTextCursor(ui->qpte->document());
cursor.setPosition(0);
ui->qpte->setTextCursor(cursor);
ui->qpte->insertHtml(text);
e1985t
  • 21
  • 2