PDA

View Full Version : QGraphicsItem, ignoring mousePressEvent if a mouseMoveEvent



nmather
30th April 2007, 20:20
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/screenies/q-macview-20070310.jpg -- the lines with arrows are what are emitted with right-click and drag )

marcel
30th April 2007, 20:43
It can be done very easily:



void Item::mousePressEvent( QGraphicsSceneMouseEvent* e)
{
mDragStartPosition = e->scenePos();
QGraphicsItem::mousePressEvent( e );
}

void Item::mouseMoveEvent( QGraphicsSceneMouseEvent* e)
{
if( (e->buttons() & Qt::RightButton == Qt::RightButton) && !mDragging )
mDragging = true;

if( mDragging )
{
//Now you're dragging
mDragEndPosition = e->scenePos();
//You have the drag start and the drag end positions.
// Now you can use them ( you can draw a line between them )
}
QGraphicsItem::mouseMoveEvent( e );
}

void Item::mouseReleaseEvent( QGraphicsSceneMouseEvent* )
{
mDragging = false;
QGraphicsItem::mouseReleaseEvent( e );
}




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

Regards

nmather
30th April 2007, 23:18
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

marcel
30th April 2007, 23:27
I disagree. I just tested your code and the sequence, when dragging nd holding the right button is:L



Mouse Pressed

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Released

Context Menu


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

nmather
30th April 2007, 23:41
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

marcel
30th April 2007, 23:45
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

nmather
30th April 2007, 23:59
Yes, with the example code that I attached, this is always the event sequence I get when doing a rightbutton drag:



Mouse Pressed

Context Menu


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

For leftbutton drag, I get:



Mouse Pressed

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Moved

Mouse Released


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