Results 1 to 5 of 5

Thread: App crash when using eventFilter

  1. #1
    Join Date
    Aug 2012
    Posts
    55
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default App crash when using eventFilter

    Hi all

    I need to change cursor when my mouse cursor is over rectangle. I tried using eventFilter but my app crash when I try to run it, can you help me with this:

    Qt Code:
    1. bool MainWindow::eventFilter(QObject *o, QEvent *e){
    2.  
    3.  
    4. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>( e );
    5. QPoint pos = mouseEvent->pos();
    6. if(rect.contains(mouseEvent->pos())){
    7. cursor.setShape(Qt::SizeFDiagCursor);}
    8.  
    9. else{
    10. cursor.setShape(Qt::ArrowCursor);}
    11. setCursor(cursor);
    12. return QMainWindow::eventFilter(o, e);}
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,690
    Thanks
    13
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: App crash when using eventFilter

    What do you think should happen in the static cast when the QEvent passed in is not a QMouseEvent? You cannot cast a QKeyEvent* to QMouseEvent* and get something meaningful. You should look at the example here.

  3. #3
    Join Date
    Aug 2012
    Posts
    55
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: App crash when using eventFilter

    I don't understand what are you trying to tell me What am I doing wrong

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,690
    Thanks
    13
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: App crash when using eventFilter

    The event filter will be called for every event, of any type, sent to the object it is filtering. This means your code will receive QKeyEvent, QResizeEvent, QFocusEvent etc. objects (and lots of them). Casting a QFocusEvent* to QMouseEvent* tells the compiler that you know what you are doing and that it should treat the data structure as a mouse event. If it is not a mouse event accessing the data through that pointer will at best give you safe rubbish and at worst crash the program. The example I linked show how you test for the type of event before you cast the pointer.

    In general, anywhere you see static_cast<> you should see danger signs. There are no safety checks other than your own.

    You may also want to consider the QWidget::mouse*Event() functions if the screen area you are interested falls within a custom widget.

  5. #5
    Join Date
    Aug 2012
    Posts
    55
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: App crash when using eventFilter

    Now I understand, thank you.

Similar Threads

  1. Eventfilter Problem
    By codeman in forum Newbie
    Replies: 6
    Last Post: 11th July 2012, 15:40
  2. Autorepeat within eventfilter
    By Patrick_Bao in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2010, 08:37
  3. QTreeWidget and eventFilter
    By luoyes in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2009, 07:38
  4. sceneEventFilter or eventFilter
    By zgulser in forum Qt Programming
    Replies: 7
    Last Post: 4th May 2009, 08:50
  5. paintEvent in eventFilter
    By kernel_panic in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 21:59

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.