Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to reposition cursor to the end of the document without autoscroll
Forum Updated to NodeBB v4.3 + New Features

How to reposition cursor to the end of the document without autoscroll

Scheduled Pinned Locked Moved Unsolved General and Desktop
49 Posts 5 Posters 8.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ JonB

    @lukutis222
    If you wait a few minutes I will knock up a tiny sample (I am Linux and Qt5,15, you never seem to have said what you are) with a QTextEdit and see whether I get the left mouse button press/release.... Come back and look in a while :)

    L Offline
    L Offline
    lukutis222
    wrote on last edited by
    #38

    @JonB Of course I will be around, I appreciate a lot.

    I use:
    ac7a2854-74c9-4721-a586-526fb897d5f6-image.png

    on my windows 10 machine

    JonBJ 1 Reply Last reply
    0
    • L lukutis222

      @JonB Of course I will be around, I appreciate a lot.

      I use:
      ac7a2854-74c9-4721-a586-526fb897d5f6-image.png

      on my windows 10 machine

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #39

      @lukutis222
      Please note that tells you your Creator was built with Qt 6.3.1, but not what version of Qt you downloaded and use for you application. Anyway at a guess it's some Qt 6. Whatever, my test will be Linux + Qt 5.15, you will have to see how that behaves for you.

      L 1 Reply Last reply
      0
      • JonBJ JonB

        @lukutis222
        Please note that tells you your Creator was built with Qt 6.3.1, but not what version of Qt you downloaded and use for you application. Anyway at a guess it's some Qt 6. Whatever, my test will be Linux + Qt 5.15, you will have to see how that behaves for you.

        L Offline
        L Offline
        lukutis222
        wrote on last edited by lukutis222
        #40

        @JonB
        Based on my directory folders I assume the project is also 6_3_1:
        b94c4bd9-7e15-4fa0-808d-21e275ee806b-image.png

        Anyways, in my application, apart from the Console I have a write box where user can write commands to the serial device:

        92d37dd9-187d-424d-81bd-7d44d2b877f5-image.png

        Just for testing purpose, I have created eventfilter2 for QLineEdit events and I can see that it behaves differently than QTextEdit:
        When I left click mouse button on the small rectangular text box at the bottom of my application, many events are generated(Just from one left mouse button click):

        event->type() = 213 
        event->type() = 71 
        event->type() = 12 
        event->type() = 170 
        event->type() = 170 
        event->type() = 110 
        event->type() = 207 
        event->type() = 8 
        event->type() = 207 
        event->type() = 207 
        event->type() = 207 
        event->type() = 2 
        mouse clicked 
        event->type() = 207 
        event->type() = 207 
        event->type() = 12 
        event->type() = 170 
        event->type() = 170 
        event->type() = 170 
        event->type() = 3 
        event->type() = 207 
        event->type() = 12 
        event->type() = 170 
        event->type() = 170 
        event->type() = 12 
        event->type() = 170 
        event->type() = 170 
        event->type() = 12 
        event->type() = 170 
        event->type() = 170 
        event->type() = 12 
        event->type() = 170 
        event->type() = 170 
        event->type() = 12 
        event->type() = 170 
        event->type() = 170 
        event->type() = 12 
        event->type() = 170 
        event->type() = 170 
        
        

        As you can see from above, one of the events were triggered mouse clicked so that seem to work (Im not sure why it is creating so many 12 170 170 events over and over again )

        JonBJ 1 Reply Last reply
        0
        • L lukutis222

          @JonB
          Based on my directory folders I assume the project is also 6_3_1:
          b94c4bd9-7e15-4fa0-808d-21e275ee806b-image.png

          Anyways, in my application, apart from the Console I have a write box where user can write commands to the serial device:

          92d37dd9-187d-424d-81bd-7d44d2b877f5-image.png

          Just for testing purpose, I have created eventfilter2 for QLineEdit events and I can see that it behaves differently than QTextEdit:
          When I left click mouse button on the small rectangular text box at the bottom of my application, many events are generated(Just from one left mouse button click):

          event->type() = 213 
          event->type() = 71 
          event->type() = 12 
          event->type() = 170 
          event->type() = 170 
          event->type() = 110 
          event->type() = 207 
          event->type() = 8 
          event->type() = 207 
          event->type() = 207 
          event->type() = 207 
          event->type() = 2 
          mouse clicked 
          event->type() = 207 
          event->type() = 207 
          event->type() = 12 
          event->type() = 170 
          event->type() = 170 
          event->type() = 170 
          event->type() = 3 
          event->type() = 207 
          event->type() = 12 
          event->type() = 170 
          event->type() = 170 
          event->type() = 12 
          event->type() = 170 
          event->type() = 170 
          event->type() = 12 
          event->type() = 170 
          event->type() = 170 
          event->type() = 12 
          event->type() = 170 
          event->type() = 170 
          event->type() = 12 
          event->type() = 170 
          event->type() = 170 
          event->type() = 12 
          event->type() = 170 
          event->type() = 170 
          
          

          As you can see from above, one of the events were triggered mouse clicked so that seem to work (Im not sure why it is creating so many 12 170 170 events over and over again )

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #41

          @lukutis222
          OK, it gets complicated. Full story probably in https://stackoverflow.com/questions/41631011/my-qt-eventfilter-doesnt-stop-events-as-it-should. I agree QTextEdit behaves differently from QLineEdit. I said you may need

          Look at installing an application event filter

          and you do.

          Here is my sample application:

          #include <QCoreApplication>
          #include <QDebug>
          #include <QLineEdit>
          #include <QTextEdit>
          #include <QVBoxLayout>
          
          #include "widget.h"
          
          Widget::Widget(QWidget *parent)
              : QWidget(parent)
          {
              QVBoxLayout *vlayout = new QVBoxLayout;
              QLineEdit *lineEdit = new QLineEdit;
              lineEdit->setObjectName("lineEdit");
              vlayout->addWidget(lineEdit);
              QTextEdit *textEdit = new QTextEdit;
              textEdit->setObjectName("textEdit");
              vlayout->addWidget(textEdit);
              setLayout(vlayout);
          
             QCoreApplication::instance()->installEventFilter(this);
             lineEdit->installEventFilter(this);
             textEdit->installEventFilter(this);
          }
          
          Widget::~Widget()
          {
          }
          
          bool Widget::eventFilter(QObject *obj, QEvent *event)
          {
          //    qDebug() << event->type();
              if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease)
                  qDebug() << "Mouse button event" << obj->objectName() << event->type();
              // pass the event on to the parent class
              return QWidget::eventFilter(obj, event);
          }
          

          Note how I use QCoreApplication::instance()->installEventFilter(this);.

          Output (from left-clicking in each of the line edit and text edit) is:

          11:17:36: Debugging starts
          Mouse button event "WidgetClassWindow" QEvent::MouseButtonPress
          Mouse button event "lineEdit" QEvent::MouseButtonPress
          Mouse button event "lineEdit" QEvent::MouseButtonPress
          Mouse button event "WidgetClassWindow" QEvent::MouseButtonRelease
          Mouse button event "lineEdit" QEvent::MouseButtonRelease
          Mouse button event "lineEdit" QEvent::MouseButtonRelease
          Mouse button event "WidgetClassWindow" QEvent::MouseButtonPress
          Mouse button event "qt_scrollarea_viewport" QEvent::MouseButtonPress
          Mouse button event "WidgetClassWindow" QEvent::MouseButtonRelease
          Mouse button event "qt_scrollarea_viewport" QEvent::MouseButtonRelease
          11:17:41: Debugging has finished
          

          See how for the QTextEdit the mouse events actually go to qt_scrollarea_viewport.

          I leave you to play with this/adapt to your needs.

          L 1 Reply Last reply
          1
          • D Offline
            D Offline
            dan1973
            wrote on last edited by
            #42

            @lukutis222
            "Yes I figured that but its not fully clear how to do it since I do not have a seperate class for QTextEdit. Can I do that in my Widget component?"

            slot function:

            QObject* objTE1 = QObject::sender();
            if (objTE1 == textEdit) { // you can catch the click or press event here. you should declare textEdit public to access.
                   <do action on mouse press or click event here>
            }
            

            you can use to catch the click or mouse press event here by connecting the signal (click or press) of the textEdit obj to any defined slot above. this way you can always catch the specific child Widget

            1 Reply Last reply
            0
            • JonBJ JonB

              @lukutis222
              OK, it gets complicated. Full story probably in https://stackoverflow.com/questions/41631011/my-qt-eventfilter-doesnt-stop-events-as-it-should. I agree QTextEdit behaves differently from QLineEdit. I said you may need

              Look at installing an application event filter

              and you do.

              Here is my sample application:

              #include <QCoreApplication>
              #include <QDebug>
              #include <QLineEdit>
              #include <QTextEdit>
              #include <QVBoxLayout>
              
              #include "widget.h"
              
              Widget::Widget(QWidget *parent)
                  : QWidget(parent)
              {
                  QVBoxLayout *vlayout = new QVBoxLayout;
                  QLineEdit *lineEdit = new QLineEdit;
                  lineEdit->setObjectName("lineEdit");
                  vlayout->addWidget(lineEdit);
                  QTextEdit *textEdit = new QTextEdit;
                  textEdit->setObjectName("textEdit");
                  vlayout->addWidget(textEdit);
                  setLayout(vlayout);
              
                 QCoreApplication::instance()->installEventFilter(this);
                 lineEdit->installEventFilter(this);
                 textEdit->installEventFilter(this);
              }
              
              Widget::~Widget()
              {
              }
              
              bool Widget::eventFilter(QObject *obj, QEvent *event)
              {
              //    qDebug() << event->type();
                  if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease)
                      qDebug() << "Mouse button event" << obj->objectName() << event->type();
                  // pass the event on to the parent class
                  return QWidget::eventFilter(obj, event);
              }
              

              Note how I use QCoreApplication::instance()->installEventFilter(this);.

              Output (from left-clicking in each of the line edit and text edit) is:

              11:17:36: Debugging starts
              Mouse button event "WidgetClassWindow" QEvent::MouseButtonPress
              Mouse button event "lineEdit" QEvent::MouseButtonPress
              Mouse button event "lineEdit" QEvent::MouseButtonPress
              Mouse button event "WidgetClassWindow" QEvent::MouseButtonRelease
              Mouse button event "lineEdit" QEvent::MouseButtonRelease
              Mouse button event "lineEdit" QEvent::MouseButtonRelease
              Mouse button event "WidgetClassWindow" QEvent::MouseButtonPress
              Mouse button event "qt_scrollarea_viewport" QEvent::MouseButtonPress
              Mouse button event "WidgetClassWindow" QEvent::MouseButtonRelease
              Mouse button event "qt_scrollarea_viewport" QEvent::MouseButtonRelease
              11:17:41: Debugging has finished
              

              See how for the QTextEdit the mouse events actually go to qt_scrollarea_viewport.

              I leave you to play with this/adapt to your needs.

              L Offline
              L Offline
              lukutis222
              wrote on last edited by
              #43

              @JonB Thanks a lot. That fixed to issue and now I am able to trigger mouse click events :)

              1 Reply Last reply
              0
              • D dan1973
                 int  column_number = ui->Console_read->textCursor().columnNumber(); // Get Col No (30)
                            qDebug("column number initial = %u \n",column_number);
                            // if cursor is in the middle, save the cursor backup, move the cursor to the end to append logs to the console and refresh the cursor to the "saved" position
                            if(column_number != 0){ 
                                cursor_backup = ui->Console_read->textCursor(); // Get Cursor
                                QTextCursor cursor = ui->Console_read->textCursor(); // Get Cursor
                                cursor.clearSelection();
                                cursor.movePosition(QTextCursor::End); // Move to End
                                ui->Console_read->setTextCursor(cursor); // set Cursor to End
                                int  column_number2 = ui->Console_read->textCursor().columnNumber(); // Get Col No (End pos)(0)
                                qDebug("column number after setTextCursor = %u \n",column_number2);
                                refresh_scrollbar = 1;
                ---
                ui->Console_read->insertPlainText(DataAsString); // Insert Text at End
                ---
                //reposition the cursor once the write is complete
                        if(refresh_scrollbar == 1){
                            qDebug("refresh cursor to the old position \n");
                            ui->Console_read->setTextCursor(cursor_backup); // Position the cursor now to saved pos (30)
                            int  column_number3 = ui->Console_read->textCursor().columnNumber(); // Get Col no
                            qDebug("column number after append = %u \n",column_number3);
                            refresh_scrollbar = 0;
                        }
                

                your cursor is working properly. it is moved to end (0) .... appends data......and then is moved back to saved pos (30).
                Now check with mouse click event inside the QTextEdit and log and see.

                L Offline
                L Offline
                lukutis222
                wrote on last edited by
                #44

                @dan1973 I am now able to check in mouse click event the position of the cursor but I am not sure how is it indended to help me:

                f9704230-e548-4c36-bbdc-e3e7290c8ae1-image.png

                bool Widget::eventFilter(QObject *object, QEvent *event)
                {
                
                
                    if ( event->type() == QEvent::MouseButtonPress)    {
                        qDebug("mouse click event \n");
                        QString str1 = "\n X:"+QString::number(QCursor::pos().x())+ "Y:" +QString::number(QCursor::pos().x());
                        qDebug("str1 = %s\n",str1.toStdString().c_str());
                    }
                
                
                    if ( object == ui->Console_read &&  ( event->type() == QEvent::MouseMove )  ) {
                        qDebug("mouse move event  \n");
                        QString str1 = "\n X:"+QString::number(QCursor::pos().x())+ "Y:" +QString::number(QCursor::pos().x());
                        qDebug("str1 = %s\n",str1.toStdString().c_str());
                    }
                
                    return false;
                }
                
                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lukutis222
                  wrote on last edited by lukutis222
                  #45

                  The main issue I have right now is when autoscroll is disabled, the cursor moves up by 1 column everytime the new data is received (of course that makes sense as the new data is received, the column number shifts up by one). However, I am yet to discover how to avoid this.

                  When I click somewhere in the middle. I would expect my cursor to stay there until I re-enable autoscroll so I can easily read logs without them moving up( which makes it complicated to read logs).

                  I think my problem is:

                              qDebug("refresh cursor to the old position \n");
                              ui->Console_read->setTextCursor(cursor_backup);
                              int  column_number3 = ui->Console_read->textCursor().columnNumber();
                              qDebug("column number after append = %u \n",column_number3);
                              refresh_scrollbar = 0;
                  

                  Instead of setting cursor to cursor_backup, I must set it to cursor_backup + offset of how many lines has the cursor moved after append in order for the cursor to maintain the position and not shift up

                  JonBJ 1 Reply Last reply
                  0
                  • L lukutis222

                    The main issue I have right now is when autoscroll is disabled, the cursor moves up by 1 column everytime the new data is received (of course that makes sense as the new data is received, the column number shifts up by one). However, I am yet to discover how to avoid this.

                    When I click somewhere in the middle. I would expect my cursor to stay there until I re-enable autoscroll so I can easily read logs without them moving up( which makes it complicated to read logs).

                    I think my problem is:

                                qDebug("refresh cursor to the old position \n");
                                ui->Console_read->setTextCursor(cursor_backup);
                                int  column_number3 = ui->Console_read->textCursor().columnNumber();
                                qDebug("column number after append = %u \n",column_number3);
                                refresh_scrollbar = 0;
                    

                    Instead of setting cursor to cursor_backup, I must set it to cursor_backup + offset of how many lines has the cursor moved after append in order for the cursor to maintain the position and not shift up

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #46

                    @lukutis222
                    Absolutely no promises, but I might go have a play with this to see if I can spot anything. Some clarifications please:

                    1. In your last post you keep talking about it moving by 1 column. You do mean 1 row, don't you? EDIT: Oh, it seems you do mean column, why are columns rather than rows involved here? Aren't you taking about appending lines/rows to the end of the QTextEdit and vertical scrolling, why are columns/horizontal scrolling at issue?
                    2. You have a QTextEdit. Your situation arises once its visual rows are filled so that it needs to scroll to display the last line, right?
                    3. "Autoscroll" is just your own implemented behaviour, right? When autoscroll is enabled you are happy that it visually jumps to the end when new data is appended?
                    4. What you want is essentially: with autoscroll disabled, user is looking at some line in the visible area. New data/line appears behind the scenes and is appended to the QTextEdit. Even if you save and restore cursor position or scroll position this causes a visual move of the preceding visible lines so that the line the user is looking it moves, and that is undesirable behaviour?
                    5. You would like a solution which allows data to be appended to the QTextEdit but leaves the visual area exactly as-was, so no move/scroll by one line or whatever?

                    It would be great if you simply answered "Yes to all of these"! :)

                    UPDATE: OK, I'm sorry but now having read through the length of this topic and all the code I am not offering to go start setting this up and investigate! But I am perplexed by your constant use of "columns" and QTextCursor::columnNumber() when you talking about appending to a QTextEdit which will cause its rows/lines to increase and vertical scrolling to occur, which is what you want to prevent? But you don't have to explain if you don't want to, I haven't investigated QTextEdit in detail....

                    I Googled qtextedit maintain scroll position, which I think is what you are trying to achieve? For vertical scrolling issue, what about say https://stackoverflow.com/a/9832270/489865 or https://stackoverflow.com/a/57975092/489865 or https://www.pythonguis.com/faq/how-to-get-set-the-position-of-the-scroll-area/, etc.? Or have you already implemented this area?

                    L 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @lukutis222
                      Absolutely no promises, but I might go have a play with this to see if I can spot anything. Some clarifications please:

                      1. In your last post you keep talking about it moving by 1 column. You do mean 1 row, don't you? EDIT: Oh, it seems you do mean column, why are columns rather than rows involved here? Aren't you taking about appending lines/rows to the end of the QTextEdit and vertical scrolling, why are columns/horizontal scrolling at issue?
                      2. You have a QTextEdit. Your situation arises once its visual rows are filled so that it needs to scroll to display the last line, right?
                      3. "Autoscroll" is just your own implemented behaviour, right? When autoscroll is enabled you are happy that it visually jumps to the end when new data is appended?
                      4. What you want is essentially: with autoscroll disabled, user is looking at some line in the visible area. New data/line appears behind the scenes and is appended to the QTextEdit. Even if you save and restore cursor position or scroll position this causes a visual move of the preceding visible lines so that the line the user is looking it moves, and that is undesirable behaviour?
                      5. You would like a solution which allows data to be appended to the QTextEdit but leaves the visual area exactly as-was, so no move/scroll by one line or whatever?

                      It would be great if you simply answered "Yes to all of these"! :)

                      UPDATE: OK, I'm sorry but now having read through the length of this topic and all the code I am not offering to go start setting this up and investigate! But I am perplexed by your constant use of "columns" and QTextCursor::columnNumber() when you talking about appending to a QTextEdit which will cause its rows/lines to increase and vertical scrolling to occur, which is what you want to prevent? But you don't have to explain if you don't want to, I haven't investigated QTextEdit in detail....

                      I Googled qtextedit maintain scroll position, which I think is what you are trying to achieve? For vertical scrolling issue, what about say https://stackoverflow.com/a/9832270/489865 or https://stackoverflow.com/a/57975092/489865 or https://www.pythonguis.com/faq/how-to-get-set-the-position-of-the-scroll-area/, etc.? Or have you already implemented this area?

                      L Offline
                      L Offline
                      lukutis222
                      wrote on last edited by lukutis222
                      #47

                      @JonB

                      1. I talk about columns because I want to maintain vertical position of the cursor. I.E when new data is appended, I want the cursor to stay in the exact same vertical position and not be shifted up. Isn't that what column is used for? Perhaps my understanding about columns and rows is wrong here.

                      2. Again you mention rows and I am thinking about columns in my head. Every new ROW of data that is appended, adds one more column to my console.

                      3. Yes. Autoscroll is my own enabled feature and the logic is very simple:

                              if(ui->autoscroll->isChecked()){
                                  QTextCursor cursor = ui->Console_read->textCursor();
                                  cursor.clearSelection();
                                  cursor.movePosition(QTextCursor::End);
                                  ui->Console_read->setTextCursor(cursor);
                              }
                      

                      I am totally happy about how my autoscroll works. I am mainly concerned when the "autoscroll" is disabled

                      1. Even if you save and restore cursor position or scroll position this causes a visual move of the preceding visible lines so that the line the user is looking it moves, and that is undesirable behaviour?
                        Yes exactly. When user disabled the autoscroll and clicks with mouse on a specific row of data, I want the QTextEdit window to "freeze" (while appending the new data at the end of course) and allow user to "inspect the data" without the console constantly moving the data upwards which makes it difficult for user to focus on that row and read it properly.

                      2. Exactly

                      I am now looking into the link that you have posted (stackoverflow) about vertical scrolling

                      JonBJ 1 Reply Last reply
                      0
                      • L lukutis222

                        @JonB

                        1. I talk about columns because I want to maintain vertical position of the cursor. I.E when new data is appended, I want the cursor to stay in the exact same vertical position and not be shifted up. Isn't that what column is used for? Perhaps my understanding about columns and rows is wrong here.

                        2. Again you mention rows and I am thinking about columns in my head. Every new ROW of data that is appended, adds one more column to my console.

                        3. Yes. Autoscroll is my own enabled feature and the logic is very simple:

                                if(ui->autoscroll->isChecked()){
                                    QTextCursor cursor = ui->Console_read->textCursor();
                                    cursor.clearSelection();
                                    cursor.movePosition(QTextCursor::End);
                                    ui->Console_read->setTextCursor(cursor);
                                }
                        

                        I am totally happy about how my autoscroll works. I am mainly concerned when the "autoscroll" is disabled

                        1. Even if you save and restore cursor position or scroll position this causes a visual move of the preceding visible lines so that the line the user is looking it moves, and that is undesirable behaviour?
                          Yes exactly. When user disabled the autoscroll and clicks with mouse on a specific row of data, I want the QTextEdit window to "freeze" (while appending the new data at the end of course) and allow user to "inspect the data" without the console constantly moving the data upwards which makes it difficult for user to focus on that row and read it properly.

                        2. Exactly

                        I am now looking into the link that you have posted (stackoverflow) about vertical scrolling

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #48

                        @lukutis222 said in How to reposition cursor to the end of the document without autoscroll:

                        Every new ROW of data that is appended, adds one more column to my console.

                        Umm, I just don't get this?! How does a row appended to the bottom of a QTextEdit have anything to do with "adding one more column"? "console constantly moving the data upwards" is to do with new rows/lines, not columns? "columns" is to do with moving cursor right/left, not up/down.

                        L 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @lukutis222 said in How to reposition cursor to the end of the document without autoscroll:

                          Every new ROW of data that is appended, adds one more column to my console.

                          Umm, I just don't get this?! How does a row appended to the bottom of a QTextEdit have anything to do with "adding one more column"? "console constantly moving the data upwards" is to do with new rows/lines, not columns? "columns" is to do with moving cursor right/left, not up/down.

                          L Offline
                          L Offline
                          lukutis222
                          wrote on last edited by lukutis222
                          #49

                          @JonB Hm yes you are right. Now that I think about it more I cant think of any logic behind this.. I think I just confused myself with this.. But I only use

                          int  column_number = ui->Console_read->textCursor().columnNumber();
                          

                          to detect if the cursor is anywhere else other than the END and that part seems to work.
                          EDIT
                          I understand now, if I click at the very beginning of a row it will still not detect but that is another issue.

                          EDIT

                          I am getting some results in the right direction (https://stackoverflow.com/questions/14550146/qtextedit-scroll-down-automatically-only-if-the-scrollbar-is-at-the-bottom/57975092#57975092)

                          void Widget::readData()
                          {
                          
                              while(serial_local->is_data_available()){
                                  //if autoscroll is checked, just move the cursor to the end of the file which will automatically cause the autoscroll to happen
                                  if(ui->autoscroll->isChecked()){
                                      QTextCursor cursor = ui->Console_read->textCursor();
                                      cursor.clearSelection();
                                      cursor.movePosition(QTextCursor::End);
                                      ui->Console_read->setTextCursor(cursor);
                                  }
                          
                                  QScrollBar *scrollbar = ui->Console_read->verticalScrollBar();
                                  bool scrollbarAtBottom  = (scrollbar->value() >= (scrollbar->maximum() - 4));
                                  int scrollbarPrevValue = scrollbar->value();
                          
                                  ui->Console_read->moveCursor(QTextCursor::End);
                                  QByteArray line = serial_local->read_data();
                                  QString DataAsString = QString(line);
                                  ui->Console_read->insertPlainText(DataAsString);
                          
                          
                                  //reposition the cursor once the write is complete
                                  if(!ui->autoscroll->isChecked())
                                  {
                                      if (scrollbarAtBottom)
                                      {
                                          ui->Console_read->ensureCursorVisible();
                                      }
                                      else
                                      {
                                          ui->Console_read->verticalScrollBar()->setValue(scrollbarPrevValue);
                                      }
                                  }
                          
                              }
                          
                          }
                          

                          When I put my cursor in the middle if QTextEdit, the data will be appended at the end but the QTextEdit will freeze in place and will not move which is exactly what I want!

                          The only issue is:

                          One of the two will happen:
                          1.

                          When I try to highlight some data(Assume I want to highlight and copy few rows of data), when the new data is appended (cursor is moved to the end of QTextEdit to append the data), all the data from the position I clicked my mouse till the end of the document will be automatically be higlighted
                          15d0c2e5-881d-46ad-985e-0e31c3c8af3c-image.png

                          2.

                          When I try to highlight some row, my action will be canceled as the new data is appended due to the cursor moving to the end again.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved