Results 1 to 7 of 7

Thread: QGraphicsItem, ignoring mousePressEvent if a mouseMoveEvent

  1. #1
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default QGraphicsItem, ignoring mousePressEvent if a mouseMoveEvent

    Hi,

    I have a situation where I'd like to ignore the behaviour of mousePressEvent on a QGraphicsItem if it turns out the user is pressing AND dragging somewhere, not just clicking the button once on the item.

    For example, I have an item that pops up a context menu with a right-click, but as a separate use case I want the user to be able to hold right-click and drag in order to draw a line out of the item. In this case I don't want the context-menu to pop up.

    I'm not sure how to achieve this, I wonder if anyone has any tips?

    (Obviously it would be easier to use a different button, but I'm porting an app and trying to maintain the same functionality )

    Thanks

    ( the items in question are in this screenshot: http://psycle.noodlemaps.net/screeni...w-20070310.jpg -- the lines with arrows are what are emitted with right-click and drag )
    Qpsycle -- open-source modular music studio built with Qt

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem, ignoring mousePressEvent if a mouseMoveEvent

    It can be done very easily:

    Qt Code:
    1. void Item::mousePressEvent( QGraphicsSceneMouseEvent* e)
    2. {
    3. mDragStartPosition = e->scenePos();
    4. QGraphicsItem::mousePressEvent( e );
    5. }
    6.  
    7. void Item::mouseMoveEvent( QGraphicsSceneMouseEvent* e)
    8. {
    9. if( (e->buttons() & Qt::RightButton == Qt::RightButton) && !mDragging )
    10. mDragging = true;
    11.  
    12. if( mDragging )
    13. {
    14. //Now you're dragging
    15. mDragEndPosition = e->scenePos();
    16. //You have the drag start and the drag end positions.
    17. // Now you can use them ( you can draw a line between them )
    18. }
    19. QGraphicsItem::mouseMoveEvent( e );
    20. }
    21.  
    22. void Item::mouseReleaseEvent( QGraphicsSceneMouseEvent* )
    23. {
    24. mDragging = false;
    25. QGraphicsItem::mouseReleaseEvent( e );
    26. }
    To copy to clipboard, switch view to plain text mode 


    But wouldn't be easier to do this and the line drawing in the QGraphicsScene, in drawForeground? You can use the same mechanism...

    Regards

  3. #3
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: QGraphicsItem, ignoring mousePressEvent if a mouseMoveEvent

    Thanks for your reply, but I couldn't get it to solve my problem.

    I've attached the code for an example program that might be able to illustrate my situation a bit better.

    If you left-click and move around, "Mouse Pressed" followed by numerous "Mouse Moved"s are printed to the terminal.

    If you right-click and move around, "Mouse Pressed" followed by "Context Event" is printed, but nothing after that. That is, no move events seem to be registered.

    I would like the right-click-drag action to ignore both the mouse-press and context-menu events, and to go straight to the mouseMoveEvent handler.

    Thanks
    Attached Files Attached Files
    Qpsycle -- open-source modular music studio built with Qt

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem, ignoring mousePressEvent if a mouseMoveEvent

    I disagree. I just tested your code and the sequence, when dragging nd holding the right button is:L

    Qt Code:
    1. Mouse Pressed
    2.  
    3. Mouse Moved
    4.  
    5. Mouse Moved
    6.  
    7. Mouse Moved
    8.  
    9. Mouse Moved
    10.  
    11. Mouse Moved
    12.  
    13. Mouse Moved
    14.  
    15. Mouse Moved
    16.  
    17. Mouse Moved
    18.  
    19. Mouse Moved
    20.  
    21. Mouse Moved
    22.  
    23. Mouse Moved
    24.  
    25. Mouse Moved
    26.  
    27. Mouse Released
    28.  
    29. Context Menu
    To copy to clipboard, switch view to plain text mode 

    As you can see this works correctly - the context menu event comes last.
    You can choose to ignore the event(no menu will be displayed ) if a drag was previously detected. You can do this with the code snipped I posted above.

    I would like the right-click-drag action to ignore both the mouse-press and context-menu events, and to go straight to the mouseMoveEvent handler.
    You must know that you cannot ignore the mouse press event and just "skip" to mouse move. But you can CHOOSE to do nothing in certain situations.

    regards

  5. #5
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: QGraphicsItem, ignoring mousePressEvent if a mouseMoveEvent

    Hi again Marcel. That's really odd, I get no "Mouse Moved" messages at all with the example when I right-click-drag, and also now that I look at it, no "Mouse Released" message, which of course should occur. Additionally, the "Context Menu" message is displayed before I release the mouse button.

    Can I ask what Qt version you are using? I am using Qt 4.2.2 on Debian Etch Linux. I wonder if this is a bug? I will try and upgrade to 4.2.3 and try again.

    Thanks
    Qpsycle -- open-source modular music studio built with Qt

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem, ignoring mousePressEvent if a mouseMoveEvent

    I am using 4.2.2 on Window (VS2003).
    Are you sure that is the events sequence you get? Because it is not normal. You should get the same sequence as for dragging with the left mouse button + context menu event.

    Have you tried this on other linux distributions?

    Anyway, if you were previously dragging( mDragging is true ), then you can ignore the context menu event, and no menu will appear.

    regards

  7. #7
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: QGraphicsItem, ignoring mousePressEvent if a mouseMoveEvent

    Yes, with the example code that I attached, this is always the event sequence I get when doing a rightbutton drag:

    Qt Code:
    1. Mouse Pressed
    2.  
    3. Context Menu
    To copy to clipboard, switch view to plain text mode 

    Additionally, the Context Menu event is triggered before I release the mouse button.

    For leftbutton drag, I get:

    Qt Code:
    1. Mouse Pressed
    2.  
    3. Mouse Moved
    4.  
    5. Mouse Moved
    6.  
    7. Mouse Moved
    8.  
    9. Mouse Moved
    10.  
    11. Mouse Moved
    12.  
    13. Mouse Released
    To copy to clipboard, switch view to plain text mode 

    which seems fine.

    I just upgraded to Qt 4.2.3 and the same sequences occur.

    I haven't tried it on another Linux distribution. I'll try to do that and then maybe I should file a bug report if it occurs on other distros.

    Thanks
    Qpsycle -- open-source modular music studio built with Qt

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  2. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23

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.