Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved Capture scroll event in textEditor

    General and Desktop
    3
    18
    896
    Loading More Posts
    • 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
      danga96 last edited by

      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-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @danga96 last edited by

        @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 Reply Quote 0
        • D
          danga96 last edited by

          Thanks for the reply; I did it like this:

          textEdit = new QTextEdit(this);
          textEdit->installEventFilter(this);
          
          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @danga96 last edited by

            @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 Reply Quote 1
            • D
              danga96 last edited by

              @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-worx 1 Reply Last reply Reply Quote 0
              • raven-worx
                raven-worx Moderators @danga96 last edited by

                @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 Reply Quote 0
                • D
                  danga96 last edited by

                  @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-worx 1 Reply Last reply Reply Quote 0
                  • raven-worx
                    raven-worx Moderators @danga96 last edited by raven-worx

                    @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 Reply Quote 1
                    • D
                      danga96 last edited by

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

                      jsulm 1 Reply Last reply Reply Quote 0
                      • jsulm
                        jsulm Lifetime Qt Champion @danga96 last edited by

                        @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 Reply Quote 0
                        • D
                          danga96 last edited by

                          @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"

                          jsulm 1 Reply Last reply Reply Quote 0
                          • jsulm
                            jsulm Lifetime Qt Champion @danga96 last edited by jsulm

                            @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 Reply Quote 1
                            • D
                              danga96 last edited by

                              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?

                              jsulm 1 Reply Last reply Reply Quote 0
                              • jsulm
                                jsulm Lifetime Qt Champion @danga96 last edited by jsulm

                                @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 Reply Quote 1
                                • D
                                  danga96 last edited by

                                  @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?

                                  jsulm 1 Reply Last reply Reply Quote 0
                                  • jsulm
                                    jsulm Lifetime Qt Champion @danga96 last edited by

                                    @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 Reply Quote 2
                                    • D
                                      danga96 last edited by

                                      @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 Reply Quote 0
                                      • D
                                        danga96 last edited by

                                        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 Reply Quote 1
                                        • First post
                                          Last post