PDA

View Full Version : Handling mouse click events in QGraphicsItem



Luc4
4th March 2010, 15:52
Hi! I'm trying to handle to mouse click event in a subclass I wrote of QGraphicsItem. The problem is that I'm able to handle correctly mousePressEvent and mouseReleaseEvent, but not clicks. I really need clicks cause I also use drag mode, so I need that, in case the user presses the mouse button, but releases after a while or drag and then releases, the press doesn't have to be interpreted as a click. Do I have to implement this myself with a timer?
Thanks!

aamer4yu
4th March 2010, 15:58
If you want clicks, then yes, you have to implement the logic yourself. QGraphicsItem doesnt have it.

Also if you just want to drag your items, you can do it simply by setting proper QGraphicsItem flags. You need not re-implement the wheel

Luc4
5th March 2010, 09:41
I tried to do this, but it is quite difficult it seems... If I handle the pressEvent to start a timer and then to do whatever it does usually, I get that no releaseEvent is thrown afterwards... Could you confirm this? So, I cannot understand whether this was a click or not... I also tried to check if a mouseMove was thrown, as this would result in that not being a click, but it seems I can't call the function of the superclass to allow the drag in the QGraphicsView... Is there any other way to implement the click event?
Thanks!

aamer4yu
5th March 2010, 09:52
Do you want the click ? Or you want dragging items ?
May be if you tell your requirement we can reply you better

Luc4
5th March 2010, 10:24
Oh, sorry. I wasn't clear. I don't want to drag items, but I want to be able to use the setDragMode to QGraphicsView::ScrollHandDrag while handling the event of clicking on an item. If the user clicks on an item, I want to do something, but in case the user presses, but moves without releasing, I want the the graphicsview to scroll as if the user pressed where no item was present. Thank you for your time!

aamer4yu
5th March 2010, 10:53
Well, you can do one thing... inherit the item, and ignore the mouse move events when the mouse is pressed.
And I guess it wont be difficult to know if the mouse was pressed, isnt it ?
Also check the chip demo in Qt demos, if you press shift after mouse press over item, you can see lines are drawn over it. similary you can ignore the events so that the view processes them and drag might work in that case .

Luc4
5th March 2010, 12:58
I still can understand how to make this work... Ignoring mouseMove events mean that I need to create a empty function in my subclass?

Luc4
5th March 2010, 16:12
Ok, I made it work. Just in case anyone else needs this, I did it by subclassing the QGraphicsScene and reimplementing there mousePressEvent and mouseReleaseEvent and using a QTime. I wasn't able to do that subclassing QGraphicsItem cause it seems mouseReleaseEvent is not called. Now it seems to work quite good.