Results 1 to 19 of 19

Thread: QTextEdit mouse movement filtering

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QTextEdit mouse movement filtering

    To filter the mouse events in a QTextEdit, do I have to subclass it and implement a custom filter to the class?
    I've been trying to add an event filter to a QTextEdit I have placed on the ui, but I can't filter mouse events. I get all other events, but input events(mouse and keyboard).

    Qt Code:
    1. moveFilter = new MoveEventFilter(ui->textEdit);
    2. ui->textEdit->setMouseTracking(true);
    3. ui->textEdit->installEventFilter(moveFilter);
    To copy to clipboard, switch view to plain text mode 

    MoveEventFilter.h:
    Qt Code:
    1. #ifndef MOVEEVENTFILTER_H
    2. #define MOVEEVENTFILTER_H
    3. #include <QtGui>
    4. #include <QDebug>
    5.  
    6. class MoveEventFilter : public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MoveEventFilter(QTextEdit *parent){
    12. //
    13. canMove = false;
    14. }
    15. bool canMove;
    16.  
    17. private:
    18. QPoint dragPosition;
    19.  
    20. protected:
    21. bool eventFilter(QObject *obj, QEvent *event)
    22. {
    23. if (event->type() == QEvent::MouseMove ){
    24. qDebug() << obj->objectName();
    25. }else{
    26. qDebug() << obj->objectName() << event->type();
    27. return QObject::eventFilter(obj, event);
    28. }
    29. }
    30. };
    31.  
    32.  
    33. #endif // MOVEEVENTFILTER_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    do I have to subclass it and implement a custom filter to the class?
    no, using another object to filter events as you are trying is also an option.

    You should at least get a compilation warning (if not an error) since your eventFilter() method doesn't return bool for all execution paths.
    If you get a MouseMove event, the function returns nothing.
    This will probably fix your problem as well.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTextEdit mouse movement filtering

    I get no errors whatsoever. It just seems that the filter can't get the mouse events.
    I did a new project, but the results are the same:

    widget.cpp:
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4. Widget::Widget(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Widget)
    7. {
    8. ui->setupUi(this);
    9.  
    10. moveFilter = new MoveEventFilter(this);
    11. ui->textEdit->setMouseTracking(true);
    12. ui->textEdit->installEventFilter(moveFilter);
    13. setMouseTracking(true);
    14. installEventFilter(moveFilter);
    15. }
    To copy to clipboard, switch view to plain text mode 

    widget.h:
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. #include "moveEventFilter.h"
    6.  
    7. namespace Ui {
    8. class Widget;
    9. }
    10.  
    11. class Widget : public QWidget {
    12. Q_OBJECT
    13. public:
    14. Widget(QWidget *parent = 0);
    15. ~Widget();
    16.  
    17. protected:
    18. void changeEvent(QEvent *e);
    19.  
    20. private:
    21. Ui::Widget *ui;
    22. MoveEventFilter *moveFilter;
    23. };
    24.  
    25. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    moveEventFilter.h:
    Qt Code:
    1. #ifndef MOVEEVENTFILTER_H
    2. #define MOVEEVENTFILTER_H
    3. #include <QtGui>
    4. #include <QDebug>
    5.  
    6. class MoveEventFilter : public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MoveEventFilter(QWidget *parent){
    12. //
    13. canMove = false;
    14. }
    15. bool canMove;
    16.  
    17. private:
    18. QPoint dragPosition;
    19.  
    20. protected:
    21. bool eventFilter(QObject *obj, QEvent *event)
    22. {
    23. if (event->type() == QEvent::MouseMove ){
    24. qDebug() << "1111111111111";
    25. return true;
    26. }else{
    27. qDebug() << obj->objectName() << event->type();
    28. return QObject::eventFilter(obj, event);
    29. }
    30. }
    31. };
    32.  
    33.  
    34. #endif // MOVEEVENTFILTER_H
    To copy to clipboard, switch view to plain text mode 

    With that code I only filter mouse moves when directly on the widget, but not on the textEdit.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    There is probably some simple error which I fail to see in your code.
    You are complicating things however.
    Usually the parent is used to filter events for children.
    This means you can pass the QTextEdit its parent as en event filter, and implement the event filter in the parent.
    This will save you the spacial class you made for this purpose.
    But, as I said, I fail to see the mistake in your code... maybe some one with sharper eyes will ;-)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    There is probably some simple error which I fail to see in your code.
    You are complicating things however.
    Usually the parent is used to filter events for children.
    This means you can pass the QTextEdit its parent as en event filter, and implement the event filter in the parent.
    This will save you the spacial class you made for this purpose.
    But, as I said, I fail to see the mistake in your code... maybe some one with sharper eyes will ;-)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTextEdit mouse movement filtering

    But using the QTextEdit's parent as a event filter (and implement it) still does not catch mouse move on the QTextEdit, only when directly on the parent:
    Qt Code:
    1. Widget::Widget(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Widget)
    4. {
    5. ui->textEdit->setMouseTracking(true);
    6. ui->textEdit->installEventFilter(this);
    7. }
    8.  
    9. void Widget::mouseMoveEvent ( QMouseEvent * event ){
    10. //bla bla bla.. code here
    11. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    I could not understand your last sentence:
    Does the code you posted last works for you or not?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTextEdit mouse movement filtering

    It doesn't. I still can't filter events on the QTextEdit.

  9. #9
    Join Date
    Jun 2010
    Location
    India
    Posts
    50
    Thanks
    1
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    To filter the mouse events in a QTextEdit, do I have to subclass it and implement a custom filter
    Yes. But you shall use your existing code with QTextEdit 's mouseMoveEvent reimplemented like the sample below. This would just propagate mouseMoveEvents and your existing event filter may work.

    Qt Code:
    1. class MyTextEdit:public QTextEdit
    2. {
    3. public:
    4. MyTextEdit(QWidget* parent):QTextEdit(parent)
    5. {
    6. }
    7. protected:
    8. void mouseMoveEvent(QMouseEvent* e)
    9. {
    10. QTextEdit::mouseMoveEvent(e);
    11. e->ignore();
    12.  
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    agathiyaa:
    sub classing is an option, but it should not be necessary, if he only wants to filter normal (not sub classed) Qt events.
    Event filtering is very easy and should work, there is a problem here that we don't see...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Jun 2010
    Location
    India
    Posts
    50
    Thanks
    1
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    @high_flyer -> My point is QWidgets mousemove events will be propogated to registered event filters, but QTextedits ( overidden) mouseMoveEvent will not propogate which in turn will not call the eventFilter. am i right ?

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    @high_flyer -> My point is QWidgets mousemove events will be propogated to registered event filters, but QTextedits ( overidden) mouseMoveEvent
    Overridden mouse events?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #13
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTextEdit mouse movement filtering

    I think he means that QTextEdit is not ment to let you filter it's mouse events, unless you subclass it. Is it that?

  14. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    Well, if that is what he means- its not true, just so we are clear about it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  15. #15
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTextEdit mouse movement filtering

    Then why can't I filter mouse moves?

  16. #16
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    because something in your code is wrong,which we can't see.
    If you create a minimal project that shows the problem and post it, I can try and run it, and see if I can find the problem.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  17. #17
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    Problem got my attention so I quickly implemented a test app.

    Qt Code:
    1. #ifndef FILTER_H
    2. #define FILTER_H
    3.  
    4. #include <QtCore>
    5. #include <QtGui>
    6. #include <QDebug>
    7. #include <QtScript>
    8.  
    9. class MoveEventFilter : public QObject
    10. { Q_OBJECT
    11.  
    12. public:
    13. MoveEventFilter(QWidget* parent = 0) : QObject(parent) {
    14. canMove = false;
    15. }
    16. bool canMove;
    17.  
    18. protected:
    19. bool eventFilter(QObject *obj, QEvent *event)
    20. {
    21. QMetaObject mo = QEvent::staticMetaObject;
    22. qDebug() << obj->objectName() << mo.enumerator(mo.indexOfEnumerator("Type")).valueToKey(event->type());
    23.  
    24. if (event->type() == QEvent::MouseMove ){
    25. QMouseEvent* me = static_cast<QMouseEvent*>(event);
    26. qDebug() << QString("(%1|%2)").arg(me->pos().x()).arg(me->pos().y());
    27. return true;
    28. }
    29.  
    30. return QObject::eventFilter(obj, event);
    31. }
    32. };
    33.  
    34. class MouseTrack : public QLabel
    35. { Q_OBJECT
    36. public:
    37. MouseTrack(QWidget* parent = 0) : QLabel(parent) {
    38. }
    39. };
    40.  
    41. #endif // FILTER_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. #include "filter.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10. QTextEdit* te = new QTextEdit();
    11. te->setObjectName("textedit");
    12.  
    13. MouseTrack* mt = new MouseTrack();
    14. mt->setMinimumHeight(100);
    15. mt->setObjectName("MouseTrack");
    16.  
    17. MoveEventFilter* filter = new MoveEventFilter();
    18.  
    19. te->setMouseTracking(true);
    20. te->installEventFilter(filter);
    21. mt->setMouseTracking(true);
    22. mt->installEventFilter(filter);
    23.  
    24. QVBoxLayout* vl = new QVBoxLayout();
    25. vl->addWidget(te,0);
    26. vl->addWidget(mt,1);
    27. w.setLayout(vl);
    28. w.show();
    29.  
    30. QObject::connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    31.  
    32. return a.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 
    If you play around with it a bit, you see that, that MouseMoves are filtered on the textedit, but only on the border!!

    I guess the main text edit is some subwidget?

    Joh

    PS: I took the time and figured out how to convert those QEvent::type codes into readable names.
    Qt Code:
    1. QEvent::staticMetaObject.enumerator(QEvent::staticMetaObject.indexOfEnumerator("Type")).valueToKey(event->type());
    To copy to clipboard, switch view to plain text mode 

  18. #18
    Join Date
    Mar 2021
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    [QUOTE=been_1990;147282]To filter the mouse events in a QTextEdit, do I have to subclass it and implement a custom filter to the class?
    I've been trying to add an event filter to a QTextEdit I have placed on the ui, but I can't filter mouse events. I get all other events, but input events(mouse and keyboard).

    After having the same problem, i finally found the solution:

    1/ The event filter need to be installed on the viewport:
    $ TextEdit->viewport()->installEventFilter(......);

    2/ In eventFilter proc, you need to catch the viewport of textEdit
    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
    if (obj == TextEdit->viewport()) {
    ....
    }
    }

  19. #19
    Join Date
    Mar 2021
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit mouse movement filtering

    Quote Originally Posted by been_1990 View Post
    To filter the mouse events in a QTextEdit, do I have to subclass it and implement a custom filter to the class?
    I've been trying to add an event filter to a QTextEdit I have placed on the ui, but I can't filter mouse events. I get all other events, but input events(mouse and keyboard).
    After having the same problem, i finally found the solution:

    Qt Code:
    1. // The event filter need to be installed on the viewport:
    2. TextEdit->viewport()->installEventFilter(......);
    3.  
    4. // In eventFilter proc, you need to catch the viewport of textEdit
    5. bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    6. {
    7. if (obj == TextEdit->viewport()) {
    8. ....
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 5
    Last Post: 27th November 2009, 13:57
  2. Forwrad & Backward mouse movement
    By sujan.dasmahapatra in forum Newbie
    Replies: 1
    Last Post: 28th October 2009, 06:54
  3. Mouse movement problem in QGraphicsView
    By zgulser in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2009, 07:01
  4. Game mouse movement
    By chaosgeorge in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2006, 23:41
  5. setCanvas blocks mouse movement on QtCanvasView
    By YuriyRusinov in forum Qt Programming
    Replies: 8
    Last Post: 20th April 2006, 07:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.