I'm writing a QT application that includes QTextBrowser
.
When the app doing some functions the function prints lines in the QTextBrowser
and during write the lines if i press the mouse left button on any printed line on the QTextBrowser
the app restart printing the lines from that line i pressed on.
how to prevent that ?
Example:
Device user: xvalid 1
Device model: alpha 16
Device name: Samsoni
Working stage: level 16
Device user: xvalid 1
Device model: alpha 16 Device name: Samsoni
Working stage: level 16
as you see the app will re-set the start writing point from where i pressed
The docs indicates that when you use insertHtml()
it is similar to:
edit->textCursor().insertHtml(fragment);
That is, the HTML is added where the cursor is and when you press with the mouse the cursor moves to the position where you click.
The solution is to move the cursor to the end:
QTextCursor cursor = your_QTextBrowser->textCursor(); // get current cursor
cursor.movePosition(QTextCursor::End); // move it to the end of the document
cursor.insertHtml(text); // insert HTML
Example:
#include <QApplication>
#include <QTextBrowser>
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextBrowser w;
int counter = 0;
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&w, &counter](){
QString text = QString::number(counter);
// at html
QTextCursor cursor = w.textCursor();
cursor.movePosition(QTextCursor::End);
cursor.insertHtml(text);
counter++;
});
timer.start(1000);
w.show();
return a.exec();
}