0

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:

  • all function output:

Device user: xvalid 1

Device model: alpha 16

Device name: Samsoni

Working stage: level 16

  • during the lines printing if i press on second line with left mouse button this will happen:

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

Community
  • 1
  • 1
John
  • 165
  • 1
  • 12
  • try with: `setReadOnly(true);` – eyllanesc Sep 02 '18 at 23:41
  • this option is only to prevent user from delete the lines not solving the issue – John Sep 02 '18 at 23:43
  • okay, I have not tried it, I'm just creating my test project, but it would be interesting to provide a [mcve], is a user ever able to edit the text? – eyllanesc Sep 02 '18 at 23:44
  • I am adding the text with `append()` and I can not reproduce your problem. How are you adding the text? – eyllanesc Sep 02 '18 at 23:46
  • No i'm using `inserthtml` to prevent adding `\n` – John Sep 02 '18 at 23:54
  • Can you provide a [mcve]? – eyllanesc Sep 02 '18 at 23:55
  • Ok just a moment – John Sep 02 '18 at 23:58
  • Edited the original post @eyllanesc – John Sep 03 '18 at 00:04
  • 1
    Is that a [mcve]? Have you read the link? – eyllanesc Sep 03 '18 at 00:05
  • Yes i read it and i need a lot of time to re-create a minimal example of the problem but i think the written example is enough and able to understand the problem – John Sep 03 '18 at 00:10
  • 1
    Well, in this case the main thing is that the problem is caused by insertHtml(). On the other hand an MCVE does not take much time, look at my solution, it is practically an MCVE. I understand the problem without needing your editing but the problem is that I could not reproduce it if you do not point out that you are using insertHtml(), so it is important that you provide an MCVE. – eyllanesc Sep 03 '18 at 00:13
  • 1
    @John, it might be enough to understand the problem, but it is somehow not nice to force everyone willing to help to create a test example in order to try to reproduce your problem, just because you don't want to spend the time to do it yourself. – scopchanov Sep 03 '18 at 00:20

1 Answers1

3

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();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    @John then that means we need an MCVE, I tried to reproduce your problem but I could not, and the reason is obvious: I do not know how you have implemented your code, a query is using `cursor.insertHtml(text);`, not `your_QTextBrowser->insertHtml(text)`. Anyway if you want help you'll have to work hard and provide a decent [mcve] :) – eyllanesc Sep 03 '18 at 00:26
  • Big thanks, the problem was solved by adding these codes `QTextCursor cursor = your_QTextBrowser->textCursor();` `cursor.movePosition(QTextCursor::End);` `cursor.insertHtml(text);` instead of `textbrowser->inserthtml` – John Sep 03 '18 at 00:34
  • 1
    @John that's my solution, I'm curious, when you said: *unfortunately not solved the issue* what code have you tried? – eyllanesc Sep 03 '18 at 00:36
  • I added only these `QTextCursor cursor = your_QTextBrowser->textCursor(); // get current cursor cursor.movePosition(QTextCursor::End); // move it to the end of the document` and added `textsbrowser->inserthtml` instead of `cursor.inserthtml`, that was my mistake, thanks again – John Sep 03 '18 at 00:38
  • @John That was my suspicion, analyze the answers before using it for it in my answer not only put code but I take the time to sustain it. – eyllanesc Sep 03 '18 at 00:42
  • I'm sorry for my mistakes, i just need very fast testing at this moment so i forget a lot of important rules and actions, thanks again for your help. – John Sep 03 '18 at 00:51