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. Capture scroll event in textEditor
QtWS25 Last Chance

Capture scroll event in textEditor

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 3 Posters 2.2k Views
  • 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.
  • D Offline
    D Offline
    danga96
    wrote on last edited by
    #1

    hello to the whole community Qt Forum,
    I would like to know if there is a way to capture the scroll event in a text editor; I have tried installing an eventfilter and captured these events:

    bool TextEdit::eventFilter(QObject* obj, QEvent* event) {
    	if (event->type() == QEvent::GraphicsSceneWheel){}
    	if (event->type() == QEvent::Wheel){}
    	if (event->type() == QEvent::Scroll){}
    	return true;
    }
    

    but they don't work, only "Wheel" is captured when you reach the beginning and the end of the scroll area
    Thanks in advance :)

    raven-worxR 1 Reply Last reply
    0
    • D danga96

      hello to the whole community Qt Forum,
      I would like to know if there is a way to capture the scroll event in a text editor; I have tried installing an eventfilter and captured these events:

      bool TextEdit::eventFilter(QObject* obj, QEvent* event) {
      	if (event->type() == QEvent::GraphicsSceneWheel){}
      	if (event->type() == QEvent::Wheel){}
      	if (event->type() == QEvent::Scroll){}
      	return true;
      }
      

      but they don't work, only "Wheel" is captured when you reach the beginning and the end of the scroll area
      Thanks in advance :)

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @danga96 said in Capture scroll event in textEditor:

      I have tried installing an eventfilter

      installed where? on the widget itself or on it's viewport widget?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • D Offline
        D Offline
        danga96
        wrote on last edited by
        #3

        Thanks for the reply; I did it like this:

        textEdit = new QTextEdit(this);
        textEdit->installEventFilter(this);
        
        raven-worxR 1 Reply Last reply
        0
        • D danga96

          Thanks for the reply; I did it like this:

          textEdit = new QTextEdit(this);
          textEdit->installEventFilter(this);
          
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @danga96
          then try
          textEdit->viewport()->installEventFilter(this);

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • D Offline
            D Offline
            danga96
            wrote on last edited by
            #5

            @raven-worx said in Capture scroll event in textEditor:

            textEdit->viewport()->installEventFilter(this);

            thanks a lot, now it goes with the mouse, but if a user uses the scrollbar the event is not captured, how can I do?

            raven-worxR 1 Reply Last reply
            0
            • D danga96

              @raven-worx said in Capture scroll event in textEditor:

              textEdit->viewport()->installEventFilter(this);

              thanks a lot, now it goes with the mouse, but if a user uses the scrollbar the event is not captured, how can I do?

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @danga96
              What are you exactly trying to achieve?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • D Offline
                D Offline
                danga96
                wrote on last edited by
                #7

                @raven-worx said in Capture scroll event in textEditor:

                What are you exactly trying to achieve?

                in practice, I would like to launch a function whenever a scroll occurs; in particular, on my text I have labels, which act as some kind of little clouds, which must follow the text, so if this scrolls upwards the label must also follow it;

                raven-worxR 1 Reply Last reply
                0
                • D danga96

                  @raven-worx said in Capture scroll event in textEditor:

                  What are you exactly trying to achieve?

                  in practice, I would like to launch a function whenever a scroll occurs; in particular, on my text I have labels, which act as some kind of little clouds, which must follow the text, so if this scrolls upwards the label must also follow it;

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #8

                  @danga96
                  then its easier to subclass QTextEdit:

                  void MyTextEdit::scrollContentsBy(int dx, int dy) {
                     // scroll happened, do whatever you want here
                     QTextEdit::scrollContentsBy(dx, dy);
                  }
                  

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  1
                  • D Offline
                    D Offline
                    danga96
                    wrote on last edited by
                    #9

                    @raven-worx
                    sorry again, but how can I use this virtual method? do i need to make a connect?

                    jsulmJ 1 Reply Last reply
                    0
                    • D danga96

                      @raven-worx
                      sorry again, but how can I use this virtual method? do i need to make a connect?

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

                      @danga96 said in Capture scroll event in textEditor:

                      do i need to make a connect?

                      No, you simply override it in your class (scrollContentsBy is called by Qt framework). This is how virtual methods are used: if a subclass overrides it then its own implementation is called, else the implementation in the base class is called.

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

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        danga96
                        wrote on last edited by
                        #11

                        @raven-worx said in Capture scroll event in textEditor:
                        @jsulm
                        thanks for answering me, but I can't get it to work; if I declare the method like this:

                        QTextEdit::scrollContentsBy(int dx,int dy);
                        

                        it works, but not belonging to the class I can't exceed the private variables;
                        do i have to do anything in .h, adding some define?
                        in the .h I have tried:

                        void scrollContentsBy(int dx, int dy) override;
                        

                        and in .cpp:

                        void TextEdit::scrollContentsBy(int dx, int dy) { }
                        

                        and I get this error in compilation: "method with override specifier 'override' did not override any base class methods"

                        jsulmJ 1 Reply Last reply
                        0
                        • D danga96

                          @raven-worx said in Capture scroll event in textEditor:
                          @jsulm
                          thanks for answering me, but I can't get it to work; if I declare the method like this:

                          QTextEdit::scrollContentsBy(int dx,int dy);
                          

                          it works, but not belonging to the class I can't exceed the private variables;
                          do i have to do anything in .h, adding some define?
                          in the .h I have tried:

                          void scrollContentsBy(int dx, int dy) override;
                          

                          and in .cpp:

                          void TextEdit::scrollContentsBy(int dx, int dy) { }
                          

                          and I get this error in compilation: "method with override specifier 'override' did not override any base class methods"

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

                          @danga96 Is your class derived from QTextEdit?

                          class MyTextEdit : public QTextEdit
                          {
                          ...
                          };
                          

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

                          1 Reply Last reply
                          1
                          • D Offline
                            D Offline
                            danga96
                            wrote on last edited by
                            #13

                            now yes, but the class already inherits from QMainWindow, and I don't know if this creates conflicts, so is there a way to do it through a connect?

                            jsulmJ 1 Reply Last reply
                            0
                            • D danga96

                              now yes, but the class already inherits from QMainWindow, and I don't know if this creates conflicts, so is there a way to do it through a connect?

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

                              @danga96 I don't get it: you want to capture scroll events in QTextEdit but in your QMainWindow? That can't work.
                              Create a new class as subclass of QTextEdit and use that subclass instead of QTextEdit...
                              Also, is MyTextEdit really derived from QMainWindow?! If so then this is really a bad name.

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

                              1 Reply Last reply
                              1
                              • D Offline
                                D Offline
                                danga96
                                wrote on last edited by
                                #15

                                @jsulm
                                I'm referring to this example: richtext-textedit
                                I'm using it to do tests; at this point I would ask you how I can adapt this with the solution proposed by you?

                                jsulmJ 1 Reply Last reply
                                0
                                • D danga96

                                  @jsulm
                                  I'm referring to this example: richtext-textedit
                                  I'm using it to do tests; at this point I would ask you how I can adapt this with the solution proposed by you?

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

                                  @danga96 The solution wasn't proposed by me.
                                  In the textedit.h file you will find this line:

                                  QTextEdit *textEdit;
                                  

                                  Replace it with your own class derived from QTextEdit:

                                  MyOwnTextEdit *textEdit;
                                  

                                  In myowntextedit.h

                                  class MyOwnTextEdit : public QTextEdit
                                  {
                                      ...
                                      void scrollContentsBy(int dx, int dy) override;
                                      ...
                                  }
                                  

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

                                  1 Reply Last reply
                                  2
                                  • D Offline
                                    D Offline
                                    danga96
                                    wrote on last edited by
                                    #17

                                    @jsulm said in Capture scroll event in textEditor:
                                    thank you very much, and I apologize in advance for the insistence, but in this way I have to modify almost everything, because I will have to create a new class and adapt all the changes I have made with this new type, only to manage the scroll event;
                                    Therefore I would like to know if there is another way.

                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      danga96
                                      wrote on last edited by
                                      #18

                                      finally I solved this way:

                                      connect(textEdit->verticalScrollBar(), &QScrollBar::valueChanged, this, &TextEdit::handleUsersCursors);
                                      	
                                      connect(textEdit->horizontalScrollBar(), &QScrollBar::valueChanged, this, &TextEdit::handleUsersCursors);
                                      

                                      without creating a new class;
                                      thanks anyway for the tips;

                                      1 Reply Last reply
                                      1

                                      • Login

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