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.2k 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.
  • L lukutis222

    @jsulm @JonB
    JonB I do not ignore and I will definately look into this (eventfilters). I just want to learn first how to correctly override subclass methods because this is very interesting topic for me and very useful to learn. I am still learning C++ and I feel like this is very important to understand (which I struggle to do yet)

    Just for the sake of testing, I have created a class with a QTextEdit base class:

    custom_text_edit.cpp

    #include "custom_text_edit.h"
    
    custom_text_Edit::custom_text_Edit()
    {
    
    }
    
    void custom_text_Edit::mousePressEvent(QMouseEvent *event)
    {
        qDebug("pressed inside qtextedit \n");
    }
    
    

    custom_text_edit.h

    #ifndef CUSTOM_TEXT_EDIT_H
    #define CUSTOM_TEXT_EDIT_H
    
    #include <QTextEdit>
    
    class custom_text_Edit : public QTextEdit
    {
    public:
        custom_text_Edit();
    
    protected:
        void mousePressEvent(QMouseEvent *event) override;
    };
    
    #endif // CUSTOM_TEXT_EDIT_H
    
    

    I create class instance in my main.cpp:

    custom_text_Edit cte;
    

    Is there anything else I am missing? When I click on the QTextEdit widget ( my console), I still cannot trigger the mouseclick event that I just overriden

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #29

    @lukutis222
    Are you 100% sure you have changed your QTextEdit, designed in Qt Designer, over to this new custom_text_Edit? How did you manage that (without promoting)?

    Which is one reason this subclassing instead of eventFilter() is going to be a pain for you.... Event filter allows you to intercept events without having to subclass, which I believe is what you requested/would like.

    1 Reply Last reply
    2
    • L Offline
      L Offline
      lukutis222
      wrote on last edited by
      #30

      @JonB @jsulm

      Ok so that is what promoting is used for! Thank you both very much I have managed to do it by promoting my console to my new custom_text_Edit class and when I click on the console I get the event triggered. I revert my console back to QTextEdit class and will now look into eventfilters

      JonBJ 1 Reply Last reply
      1
      • L lukutis222

        @JonB @jsulm

        Ok so that is what promoting is used for! Thank you both very much I have managed to do it by promoting my console to my new custom_text_Edit class and when I click on the console I get the event triggered. I revert my console back to QTextEdit class and will now look into eventfilters

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #31

        @lukutis222
        Note that Qt has two means of "notifying" that something has happened. Events are just protected methods and require subclassing if you need to know about them . Signals on the other hand are public methods and you can attach slots to be notified of their occurrence without needing to subclass. (Note also that in an event you can alter the behaviour of the handling of the event, if you wish to, but in a signal you cannot, the action has already happened and you can only add your own behaviour not alter the action's existing behaviour.) For example, mouse up/down are events, but "click" is a signal; same for key presses as opposed to, say, the textChanged() signal after a down/up. Nobody knows the official definition/determination of when an action is exposed as a signal rather than an event, but basically the lowest level activities are events and higher level ones are signals.

        eventFilter() is neither an event nor a signal. It is a method you can install as a "hook" for any low level event which may arise, allowing you either to just monitor it (as in your case) or alter what happens to it before further Qt processing. It conveniently allows any event to any widget to be intercepted without needing to subclass.

        D 1 Reply Last reply
        3
        • JonBJ JonB

          @lukutis222
          Note that Qt has two means of "notifying" that something has happened. Events are just protected methods and require subclassing if you need to know about them . Signals on the other hand are public methods and you can attach slots to be notified of their occurrence without needing to subclass. (Note also that in an event you can alter the behaviour of the handling of the event, if you wish to, but in a signal you cannot, the action has already happened and you can only add your own behaviour not alter the action's existing behaviour.) For example, mouse up/down are events, but "click" is a signal; same for key presses as opposed to, say, the textChanged() signal after a down/up. Nobody knows the official definition/determination of when an action is exposed as a signal rather than an event, but basically the lowest level activities are events and higher level ones are signals.

          eventFilter() is neither an event nor a signal. It is a method you can install as a "hook" for any low level event which may arise, allowing you either to just monitor it (as in your case) or alter what happens to it before further Qt processing. It conveniently allows any event to any widget to be intercepted without needing to subclass.

          D Offline
          D Offline
          dan1973
          wrote on last edited by
          #32

          @JonB Wow!!

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lukutis222
            wrote on last edited by lukutis222
            #33

            @JonB Thank you very much for all this useful information. After reading a little about bit about the eventfilters and looking at some examples (https://forum.qt.io/topic/110350/how-to-make-an-eventfilter/2), I have changed my implemented eventfilter in my widget component.

            In my widget.cpp, I have added:

            bool Widget::eventFilter(QObject *object, QEvent *event)
            {
                //qDebug("event filter \n");
            
                if ((object == ui->Console_read) && (event->type() == QEvent::MouseButtonPress) )   {
                    qDebug("mouse clicked \n");
                }
                if ( object == ui->Console_read &&  ( event->type() == QEvent::MouseButtonDblClick )  ) {
                    qDebug("mouse double click \n");
                }
            
            
            
                return false;
            }
            

            And in the widget constructor:

            ui->Console_read->installEventFilter(this); // console read is the QTextEdit window that I would like to detect mouse clicks on
            

            in my widget.h
            I have added function prototype under the private section:

            private:
                Ui::Widget *ui;
                bool eventFilter(QObject *object, QEvent *event);
            
            

            However, it does not seem to work as I expected. I was expecting the below to be triggered when I click anywhere on the console

                if ((object == ui->Console_read) && (event->type() == QEvent::MouseButtonPress) )   {
                    qDebug("mouse clicked \n");
                }
            

            and below to be triggered when I double click anywhere on the console.

                if ( object == ui->Console_read &&  ( event->type() == QEvent::MouseButtonDblClick )  ) {
                    qDebug("mouse double click \n");
                }
            

            However, the event filter does not react to the left mouse button click and double left mouse button click.

            When I right click mouse button, the event filter triggers MouseButtonPress. Why would it react to right click for MouseButtonPress Is that default? Do I need to override the MouseButtonPress?

            jsulmJ JonBJ 2 Replies Last reply
            0
            • L lukutis222

              @JonB Thank you very much for all this useful information. After reading a little about bit about the eventfilters and looking at some examples (https://forum.qt.io/topic/110350/how-to-make-an-eventfilter/2), I have changed my implemented eventfilter in my widget component.

              In my widget.cpp, I have added:

              bool Widget::eventFilter(QObject *object, QEvent *event)
              {
                  //qDebug("event filter \n");
              
                  if ((object == ui->Console_read) && (event->type() == QEvent::MouseButtonPress) )   {
                      qDebug("mouse clicked \n");
                  }
                  if ( object == ui->Console_read &&  ( event->type() == QEvent::MouseButtonDblClick )  ) {
                      qDebug("mouse double click \n");
                  }
              
              
              
                  return false;
              }
              

              And in the widget constructor:

              ui->Console_read->installEventFilter(this); // console read is the QTextEdit window that I would like to detect mouse clicks on
              

              in my widget.h
              I have added function prototype under the private section:

              private:
                  Ui::Widget *ui;
                  bool eventFilter(QObject *object, QEvent *event);
              
              

              However, it does not seem to work as I expected. I was expecting the below to be triggered when I click anywhere on the console

                  if ((object == ui->Console_read) && (event->type() == QEvent::MouseButtonPress) )   {
                      qDebug("mouse clicked \n");
                  }
              

              and below to be triggered when I double click anywhere on the console.

                  if ( object == ui->Console_read &&  ( event->type() == QEvent::MouseButtonDblClick )  ) {
                      qDebug("mouse double click \n");
                  }
              

              However, the event filter does not react to the left mouse button click and double left mouse button click.

              When I right click mouse button, the event filter triggers MouseButtonPress. Why would it react to right click for MouseButtonPress Is that default? Do I need to override the MouseButtonPress?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #34

              @lukutis222 Print out what event->type() returns in your eventFilter and see what you get when you press left mouse button. For left mouse button click you should get two events: QEvent::MouseButtonPress and QEvent::MouseButtonRelease.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              L 1 Reply Last reply
              0
              • L lukutis222

                @JonB Thank you very much for all this useful information. After reading a little about bit about the eventfilters and looking at some examples (https://forum.qt.io/topic/110350/how-to-make-an-eventfilter/2), I have changed my implemented eventfilter in my widget component.

                In my widget.cpp, I have added:

                bool Widget::eventFilter(QObject *object, QEvent *event)
                {
                    //qDebug("event filter \n");
                
                    if ((object == ui->Console_read) && (event->type() == QEvent::MouseButtonPress) )   {
                        qDebug("mouse clicked \n");
                    }
                    if ( object == ui->Console_read &&  ( event->type() == QEvent::MouseButtonDblClick )  ) {
                        qDebug("mouse double click \n");
                    }
                
                
                
                    return false;
                }
                

                And in the widget constructor:

                ui->Console_read->installEventFilter(this); // console read is the QTextEdit window that I would like to detect mouse clicks on
                

                in my widget.h
                I have added function prototype under the private section:

                private:
                    Ui::Widget *ui;
                    bool eventFilter(QObject *object, QEvent *event);
                
                

                However, it does not seem to work as I expected. I was expecting the below to be triggered when I click anywhere on the console

                    if ((object == ui->Console_read) && (event->type() == QEvent::MouseButtonPress) )   {
                        qDebug("mouse clicked \n");
                    }
                

                and below to be triggered when I double click anywhere on the console.

                    if ( object == ui->Console_read &&  ( event->type() == QEvent::MouseButtonDblClick )  ) {
                        qDebug("mouse double click \n");
                    }
                

                However, the event filter does not react to the left mouse button click and double left mouse button click.

                When I right click mouse button, the event filter triggers MouseButtonPress. Why would it react to right click for MouseButtonPress Is that default? Do I need to override the MouseButtonPress?

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #35

                @lukutis222
                I don't know why you are not getting the left mouse button, especially as you say you do for the right button. Like @jsulm I would expect a mouse press and a mouse release. Start by removing your object == ui->Console_read &&, let's see whether you get these anywhere regardless of object? Also do as @jsulm says: log every event arriving in your eventFilter(), see if there is anything else there of interest.

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @lukutis222 Print out what event->type() returns in your eventFilter and see what you get when you press left mouse button. For left mouse button click you should get two events: QEvent::MouseButtonPress and QEvent::MouseButtonRelease.

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

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

                  Print out what event->type(

                  When I hover my mouse over console, events 10 and 110 are generated

                  When cursor leaves the console area, event 11 is generated

                  When I left click on my console, event = 207 is generated.

                  When I click left mouse button multiple times(double click), multiple 207 events are generated:

                  event->type() = 207 
                  event->type() = 207 
                  event->type() = 207 
                  event->type() = 207 
                  

                  Right mouse button click generates 4 events:

                  event->type() = 2 
                  mouse clicked 
                  event->type() = 9 
                  event->type() = 12 
                  event->type() = 11 
                  
                  bool Widget::eventFilter(QObject *object, QEvent *event)
                  {
                  
                      qDebug("event->type() = %u \n",event->type());
                      if ((object == ui->Console_read) && (event->type() == QEvent::MouseButtonPress) )   {
                          qDebug("mouse clicked \n");
                      }
                  
                  
                      if ( object == ui->Console_read &&  ( event->type() == QEvent::MouseButtonDblClick )  ) {
                          qDebug("mouse double click \n");
                      }
                  
                      return false;
                  }
                  

                  069a7e14-c50b-432f-80a9-abd570774a99-image.png

                  Additionally, I tried to remove object == ui->Console_read but does not have any different affect

                  JonBJ 1 Reply Last reply
                  0
                  • L lukutis222

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

                    Print out what event->type(

                    When I hover my mouse over console, events 10 and 110 are generated

                    When cursor leaves the console area, event 11 is generated

                    When I left click on my console, event = 207 is generated.

                    When I click left mouse button multiple times(double click), multiple 207 events are generated:

                    event->type() = 207 
                    event->type() = 207 
                    event->type() = 207 
                    event->type() = 207 
                    

                    Right mouse button click generates 4 events:

                    event->type() = 2 
                    mouse clicked 
                    event->type() = 9 
                    event->type() = 12 
                    event->type() = 11 
                    
                    bool Widget::eventFilter(QObject *object, QEvent *event)
                    {
                    
                        qDebug("event->type() = %u \n",event->type());
                        if ((object == ui->Console_read) && (event->type() == QEvent::MouseButtonPress) )   {
                            qDebug("mouse clicked \n");
                        }
                    
                    
                        if ( object == ui->Console_read &&  ( event->type() == QEvent::MouseButtonDblClick )  ) {
                            qDebug("mouse double click \n");
                        }
                    
                        return false;
                    }
                    

                    069a7e14-c50b-432f-80a9-abd570774a99-image.png

                    Additionally, I tried to remove object == ui->Console_read but does not have any different affect

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #37

                    @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 1 Reply Last reply
                    0
                    • 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 Online
                        JonBJ Online
                        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 Online
                            JonBJ Online
                            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 Online
                                      JonBJ Online
                                      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 Online
                                          JonBJ Online
                                          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

                                          • Login

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