Capture scroll event in textEditor
-
@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;
-
@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); }
-
@raven-worx
sorry again, but how can I use this virtual method? do i need to make a connect? -
@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.
-
@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"
-
@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. -
@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; ... }
-
@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. -
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;