Results 1 to 6 of 6

Thread: Problem in Mouse Events

  1. #1
    Join Date
    Apr 2016
    Posts
    5
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Question Problem in Mouse Events

    I have a QWidget with a QGraphicsView and a push button. The QGraphicsView has to take mouse press and release events to detect a swipe .At the same time push button should run a small function on clicked. I used an event filter in the QWidget to detect the mouse events.

    Qt Code:
    1. bool Widget::eventFilter(QObject * obj, QEvent * event)
    2. {
    3.  
    4. // Capturing keyboard events for moving
    5. if( event->type() == QEvent::KeyPress )
    6. {
    7. //Do something
    8. }
    9.  
    10. //Capturing mouse events for swipe
    11. else if( event->type() == QEvent::MouseButtonPress)
    12. {
    13. QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
    14. swipe_startPoint = mouseEvent->pos();
    15. }
    16.  
    17. else if( event->type() == QEvent::MouseButtonRelease)
    18. {
    19. QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
    20. swipe_endPoint = mouseEvent->pos();
    21. swipeDirection();
    22. }
    23.  
    24. else
    25. {
    26. QWidget::eventFilter(obj,event);
    27. }
    To copy to clipboard, switch view to plain text mode 

    In the constructor of the Widget class i have the following
    Qt Code:
    1. ui->graphicsView->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 


    The problem is that the button is getting clicked but the MouseButtonRelease event that sets the 'swipe_endPoint' value is not working.

    When i set
    Qt Code:
    1. ui->graphicsView->grabMouse();
    To copy to clipboard, switch view to plain text mode 
    the mouse pressed and released events are working perfectly but the button stops accepting the events.

    Note : Using QT 5.6.0 in Ubuntu

    Can you please help me overcome this problem.
    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem in Mouse Events

    Try installing the event filter in the graphicsview's viewport widget.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    5
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Problem in Mouse Events

    Thanks a lot for your input . I replace the code as following
    Qt Code:
    1. ui->graphicsView->viewport()->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    . After this the graphicView stopped taking mouse events

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem in Mouse Events

    Are you returning false in your event filter when you are not handling the event?

    Cheers,
    _

  5. #5
    Join Date
    Apr 2016
    Posts
    5
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Problem in Mouse Events

    No, where shall i do that?

    In my code if the event is not taken it goes to the else block of the eventFilter() and the event is passed to QWidget::eventFilter(obj,event);

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem in Mouse Events

    Quote Originally Posted by anirudh123 View Post
    No, where shall i do that?
    The eventFilter() method needs to return either true or false, depending on whether it has consumed the event or not.

    Quote Originally Posted by anirudh123 View Post
    In my code if the event is not taken it goes to the else block of the eventFilter() and the event is passed to QWidget::eventFilter(obj,event);
    Maybe you could post the actual code?
    The snippet you posted earlier does not return anything in any of the other branches. If that results in eventFilter() being evaluated as true, then mouse press/release and key press never make it to the actual target.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 18th December 2011, 11:59
  2. Replies: 7
    Last Post: 21st January 2011, 19:53
  3. problem propagating mouse events.
    By ngc_1729 in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2010, 14:11
  4. Problem in Mouse Press Events & Vectors
    By dheeraj in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2008, 18:08
  5. problem about mouse button events
    By vql in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2008, 10:14

Tags for this Thread

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.