Results 1 to 6 of 6

Thread: QGraphicsView Mouse Events

  1. #1
    Join Date
    Jul 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsView Mouse Events

    Hi!

    I'm having problems with my QGraphicsView and some mouse events. Basically I want to zoom and pan my view and also handle the items events.

    I want the following behavior: The graphics view should only receive the events while my space button is pressed. So if you want to pan/zoom you should press space and either the right or left mouse button (left = pan, right = zoom). Otherwise the items should receive the events.

    My items don't implement any event handling at the moment so they are using the standard implementation.

    The code for my view:

    Qt Code:
    1. //--------------------------------------------------------------------------------
    2. void GraphicsView::mousePressEvent(QMouseEvent *event)
    3. {
    4. if(_key != Qt::Key_Space)
    5. {
    6. QGraphicsView::mousePressEvent(event);
    7. event->ignore();
    8. return;
    9. }
    10.  
    11. //save mouse position
    12. _last_position = event->pos();
    13. }
    14.  
    15. //--------------------------------------------------------------------------------
    16. void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
    17. {
    18. if(_key != Qt::Key_Space)
    19. {
    20. QGraphicsView::mouseReleaseEvent(event);
    21. event->ignore();
    22. return;
    23. }
    24.  
    25. setCursor(_default_cursor);
    26. }
    27.  
    28. //--------------------------------------------------------------------------------
    29. void GraphicsView::mouseMoveEvent(QMouseEvent *event)
    30. {
    31. if(_key != Qt::Key_Space)
    32. {
    33. QGraphicsView::mouseMoveEvent(event);
    34. event->ignore();
    35. return;
    36. }
    37.  
    38. int dx = event->x() - _last_position.x();
    39. int dy = event->y() - _last_position.y();
    40.  
    41. //save mouse position
    42. _last_position = event->pos();
    43.  
    44. qDebug() << "pos: " << event->pos().x() << " " << event->pos().y();
    45.  
    46. if (event->buttons() == Qt::LeftButton)
    47. {
    48. setCursor(_move_cursor);
    49. pan(dx, dy);
    50. }
    51. else if (event->buttons() == Qt::RightButton)
    52. {
    53. zoom(dx);
    54. }
    55. }
    56.  
    57. //--------------------------------------------------------------------------------
    58. void GraphicsView::keyPressEvent(QKeyEvent *event)
    59. {
    60. _key = event->key();
    61. }
    62.  
    63. //--------------------------------------------------------------------------------
    64. void GraphicsView::keyReleaseEvent(QKeyEvent *event)
    65. {
    66. _key = 0;
    67. }
    To copy to clipboard, switch view to plain text mode 

    Now my main problem is that I receive all events as expected but during my views mousMoveEvent which I handle only while space is pressed my events position isn't correct. It stays always the same. If I don't have space pressed the events position changes depending on the mouse position.

    I have no idea what I'm doing wrong. If I uncomment all the QGraphicsView::mouseXEvent(event) it works fine. But if I deliver the events to my items if space is not pressed it doesn't work.

  2. #2
    Join Date
    Jul 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView Mouse Events

    nobody?

  3. #3
    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: QGraphicsView Mouse Events

    I suggest you manipulate with QGraphicsView::ScrollHandDrag a bit. And in your event handlers change modes and let the events flow as usual - simply act depending on the current mode instead of ignoring events, that's not a good way to go.

    Panning you'll have for free using aforementioned QGraphicsView::ScrollHandDrag. For zooming you'll have to implement a separate mode which you'll be handling in an event or slot that is called when a specific action you want zooming to be triggered with is performed (for instance wheelEvent is you want zooming to be triggered by the mouse wheel).

  4. #4
    Join Date
    Jul 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView Mouse Events

    What do you mean with "act depending on the current mode"? What mode? How can I change the "mode"?

    Also the problem with the ScrollHandDrag is that it works fine but only if I click and drag on an area where no GraphicItems are. If my mouse is over an item it will click it. Since my scene will be full with items I want to prevent the click while my space button is pressed.

    Thx!

  5. #5
    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: QGraphicsView Mouse Events

    Quote Originally Posted by tomf View Post
    What do you mean with "act depending on the current mode"?
    If your "mode" is "pan" then handle panning and ignore everything else. If the mode is "zoom" then handle zooming and ignore everything else, etc.

    What mode? How can I change the "mode"?
    You have to implement it by reimplementing events, for instance:

    Qt Code:
    1. void MyView::keyPressEvent(QKeyEvent *ke){
    2. if(ke->key()==Qt::Key_Space)
    3. m_spaceOn = true;
    4. QGraphicsView::keyPressEvent(ke);
    5. }
    6. void MyView::keyReleaseEvent(QKeyEvent *ke){
    7. if(ke->key()==Qt::Key_Space)
    8. m_spaceOn = false;
    9. QGraphicsView::keyReleaseEvent(ke);
    10. }
    11. void MyView::mousePressEvent(QMouseEvent *me){
    12. if(m_spaceOn && me->button()==Qt::RightButton)
    13. m_mode = Zooming;
    14. if(m_spaceOn && me->button()==Qt::LeftButton){
    15. m_mode = Panning;
    16. setDragMode(QGraphicsView::ScrollHandDrag);
    17. }
    18. QGraphicsView::mousePressEvent(me);
    19. }
    20.  
    21. void MyView::mouseReleaseEvent(QMouseEvent *me){
    22. if(m_spaceOn)
    23. m_mode = None;
    24. setDragMode(QGraphicsView::NoDrag);
    25. }
    26. QGraphicsView::mouseReleaseEvent(me);
    27. }
    28.  
    29. void MyView::mouseMoveEvent(QMouseEvent *me){
    30. // panning is handled by DragMode
    31. if(m_mode==Zooming){
    32. // handle zooming here
    33. }
    34. QGraphicsView::mouseMoveEvent(me);
    35. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jul 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: QGraphicsView Mouse Events

    Ok so I finally found the solution to my problem.

    Since I didn't want to use the ScrollHandDrag mode I always got the problem that my mouse events were a bit "off". Now I discovered why: the function scrollContentsBy in qgraphicsview always re-sends the last mouse move event. Since I always saved my current/last mouse position in the mouse move event and calculated the difference and used that as the amount I got wrong values.

    My solution: before calling my zoom or translate function call setInteractive(false). This prevents the scrollContentsBy function of re-sending the event. Afterwards set it back to true.

Similar Threads

  1. QGraphicsView mouse events
    By high_flyer in forum Qt Programming
    Replies: 22
    Last Post: 29th May 2014, 09:03
  2. Replies: 9
    Last Post: 22nd June 2008, 22:26
  3. Handling Mouse Events of a parent widget
    By dvmorris in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2007, 18:44
  4. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25

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.