Results 1 to 11 of 11

Thread: EventFilter and MouseMoveEvent

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default EventFilter and MouseMoveEvent

    Hi, I installed an event filter on a PushButton in MainForm; Pushbutton call an QFileDialog
    in wich I can choose file. If I choose a file with doubleClick, QFileDIalog is closing but
    start MYWidget::MouseMoveEvent.
    Qt Code:
    1. loadButton->installEventFilter(myWidget1);
    To copy to clipboard, switch view to plain text mode 

    I tried to filter out only mosue events but filtering is not executed. If I chancge below
    QMouseEvent* in QEvent*: it works and filters out some event but none QMouseEvent.
    I tried to not remove filter on loadButton and don't change....
    Qt Code:
    1. bool MyWidget::eventFilter(QObject* obj, QMouseEvent* e) {
    2. cout << "filter\n";
    3. //if (obj == w->loadTexture) {
    4. cout << e->type() << endl;
    5. if (( e->type() != QMouseEvent::None)) {
    6. cout << "filter out\n";
    7. return TRUE;
    8. }
    9. else {
    10. return FALSE;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    thanks
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: EventFilter and MouseMoveEvent

    Qt Code:
    1. bool MyWidget::eventFilter(QObject* obj, QMouseEvent* e)
    To copy to clipboard, switch view to plain text mode 

    The event filter method must use QEvent and not QMouseEvent. It is one of the methods of QWidget (or even QObject) and you are overloading it here, you can't create new methods with the same name and different arguments, because it will simply not be called. This isn't any "magic", it's plain C++.

    If you wish to filter mouse events, you have to compare the type of the event received to the type you wish to check and then react.

    Qt Code:
    1. bool MyWidget::eventFilter(QObject* obj, QEvent* e) {
    2. cout << e->type() << endl;
    3. if (( e->type() == QEvent::MouseMove)) {
    4. cout << "filter out\n";
    5. return TRUE;
    6. }
    7. return QWidget::eventFilter(obj,e);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Remember this will reject ANY mouse move event. You should check for the object too and be sure you really want to handle a particular event in the filter.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default EventFilter and MouseMoveEvent

    Quote Originally Posted by wysota
    Qt Code:
    1. bool MyWidget::eventFilter(QObject* obj, QMouseEvent* e)
    To copy to clipboard, switch view to plain text mode 

    If you wish to filter mouse events, you have to compare the type of the event received to the type you wish to check and then react.

    Qt Code:
    1. bool MyWidget::eventFilter(QObject* obj, QEvent* e) {
    2. cout << e->type() << endl;
    3. if (( e->type() == QEvent::MouseMove)) {
    4. cout << "filter out\n";
    5. return TRUE;
    6. }
    7. return QWidget::eventFilter(obj,e);
    8. }
    To copy to clipboard, switch view to plain text mode 
    I proved to change QMouseEvent in QEvent but don't change.
    Qt Code:
    1. cout << e->type() << endl;
    To copy to clipboard, switch view to plain text mode 
    This above says me wich types of events filter out: Paint = 12,FocusIn = 8...but
    don't appear: MouseButtonPress = 2, MouseButtonRelease = 3, MouseButtonDblClick = 4,MouseMove = 5.
    And how MouseMove is Called?I know is called because I insert a printf in MouseMoveEvent.....
    Regards

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default EventFilters

    Hi,
    I proved to change QMouseEvent in QEvent (such as below) but don't change.
    Qt Code:
    1. bool MyWidget::eventFilter(QObject* obj, QEvent* e) {
    2. cout << e->type() << endl;
    3. if (( e->type() == QEvent::MouseMove)) {
    4. cout << "filter out\n";
    5. return TRUE;
    6. }
    7. return QWidget::eventFilter(obj,e);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. cout << e->type() << endl;
    To copy to clipboard, switch view to plain text mode 
    This above says me wich types of events are passed in eventfilter: Paint = 12,FocusIn = 8...but don't appear: MouseButtonPress = 2, MouseButtonRelease = 3, MouseButtonDblClick = 4,MouseMove = 5.
    And how MouseMove is Called?I know is called because I insert a printf in MouseMoveEvent....
    I don't understand why MouseMove is not filter out (mousemove don't pass in eventFilter....)
    Thanks
    Last edited by mickey; 25th January 2006 at 21:50.
    Regards

  5. #5
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: EventFilters

    How do you install the event filter? How many widgets do you have at all? Maybe your filtered widget is obscured by another one so you get only some events.
    It's nice to be important but it's more important to be nice.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EventFilters

    Quote Originally Posted by mickey
    but don't appear: MouseButtonPress = 2, MouseButtonRelease = 3, MouseButtonDblClick = 4,MouseMove = 5.
    And how MouseMove is Called?
    Don't you need to enable mouse tracking to get these?

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: EventFilters

    Hi,
    I link a loadButton to a SLOT:
    Qt Code:
    1. MainfForm::load {
    2. loadButton->installEventFilter(this->myWidget1);
    3. loadButton->installEventFilter(this->myWidget2);
    4. loadButton->installEventFilter(this->myWidget3);
    5. ......
    6. QString te = fd.getOpenFileName(xdir.path()+"/"+" ....",
    7. ...........
    8. }
    To copy to clipboard, switch view to plain text mode 
    Then:
    Qt Code:
    1. bool MyWidget::eventFilter(QObject* obj, QEvent* e) {
    2. cout << "filter\n";
    3. cout << e->type() << endl;
    4. if ( e->type() == QEvent::Move) {
    5. cout << "filter out\n";
    6. return TRUE;
    7. }
    8. return QGLWidget::eventFilter(obj,e);
    9. }
    To copy to clipboard, switch view to plain text mode 
    I have 3 instance of MyWidget. The MouseMoveEvent in myWidget start when I choose a file "with doubleClick" in QFileDialog but printf inside QMouseEvent (in MyWidget) says
    that only mouseMove start...

    A hint? Thanks
    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: EventFilters

    did you enable mouse tracking ??? Basically a widget records mose move only when a click occurs...

    You can change that settings :
    Qt Code:
    1. setMouseTracking(true) ;
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: EventFilters

    Quote Originally Posted by fullmetalcoder
    did you enable mouse tracking ??? Basically a widget records mose move only when a click occurs...

    You can change that settings :
    Qt Code:
    1. setMouseTracking(true) ;
    To copy to clipboard, switch view to plain text mode 
    yes I proved.Don't change...I'm thinking that QmouseMoveEvent starts for some strange
    reason.......
    Regards

  10. #10
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: EventFilters

    Because you tried to contact me via skype and sending message failed, here's what I said there:

    It would help if you'd provide a full working example with source as short as possible so I can try on my machine. I and others already suggested what could be the reason. If none of our tipps work, we have to see code. Often while you create such a like 100 LOC application, you solve the problem because the problem does not occur in the small testapp. Then you will compare what is different and hopefully find the reason. If not, post the programm
    It's nice to be important but it's more important to be nice.

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: EventFilters (SOLVED)

    axeljaeger, thanks for reply...
    I solved my problem but I don't understand why qMouseMoveEvent of MyWidget:: starts
    starts after the code of loadButton():
    Qt Code:
    1. void MainForm::loadbutton()
    2. {
    3. this->myWidget->installEventFilter(this->myWidget);
    4. .............
    5. printf("end mainform()LoadButt\n");
    6. }
    To copy to clipboard, switch view to plain text mode 
    (With the printf above I see that QMouseMoveEvent of myWidget starts after (out) loadButtton(). But Why? I coded removeFilter at the end of loadbutton() but QmouseMoveEvent starts after and so don't filter it)

    Qt Code:
    1. bool MyWidget::eventFilter(QObject* obj, QEvent* e) {
    2. if (obj == this) {
    3. cout << e->type() << endl;
    4. if ( e->type() == QEvent::MouseMove) {
    5. this->removeEventFilter(this);
    6. cout << "filter out\n";
    7. return TRUE;
    8. }
    9. else {
    10. return QGLWidget::eventFilter(obj,e);
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    With cout << e->type I see all QEvent on myWidget and MouseMove runs only after the oadButton()...so I filter out only one (setMouseTracking is off) QMouseMove and I put removeFilter in the eventFilter (but I don't LIKE this....) .
    What do you think of this?
    thanks
    Regards

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.