How to handle QGraphicsItem mouse events in QGraphicsView
hi all,
I wrote some objects derived from QGraphicsView and QGraphicsItem, and I simulate a mouse click event in my QGraphicsItem just like this
Code:
{
// if(e->buttons() == Qt::LeftButton)
// SetButtonState(NORMAL);
setSelected(false); // i don't need the focus
QRectF bRect
= boundingRect
();
if(bRect.contains(lastpt))
; // ??? It's a click event.
}
My question is, how can i track the mouse 'click' event in my QGraphicsView? in other words, i need to integrate these mouse events in QGraphicsView and find which item activate it,
Code:
void QMyGraphicsView::Command( param)
{
switch(param)
{
case ?? : ...
case ?? : ...
}
...
}
or is there a better place to dispose other than QGraphicsView?
thanks for any advice.
Re: How to handle QGraphicsItem mouse events in QGraphicsView
Quote:
My question is, how can i track the mouse 'click' event in my QGraphicsView?
For that you will need to properly handle mouse press and mouse release event of the graphics item and then emit a signal from the item.
Thats how things are done in normal pushbuttons also. But for graphics item, you will need to decide when you want to emit the clicked() signal... on mouse press, on mouse release, duration between the press and release, etc.
Re: How to handle QGraphicsItem mouse events in QGraphicsView
One good place to do it is in the QGraphicsScene, QGraphicsScene::mouseGrabberItem() will return the item that accepted a mousePressEvent (at the release it will not return it though) .
Another one is QGraphicsScene::focusItem() which returns the item that is focused.
Or in the QGraphicsView, when you got mouseReleaseEvent get the mouse position and then pass it to QGraphicsView::itemAt(const QPoint & pos).
You can do the same in QGraphicsScene with QGraphicsScene::itemAt(...) (will return the topmost visible item at that position) or QGraphicsScene::items(...) (you'll get all the items at that position)
Re: How to handle QGraphicsItem mouse events in QGraphicsView
Thank you guys.
QGraphicsItem is not QObject-based , but how i emit the signal to its parentview? I'll try 'QGraphicsScene::mouseGrabberItem()' first.
I'm a two-days Qt'er, thanks for your reply.
Re: How to handle QGraphicsItem mouse events in QGraphicsView
Quote:
QGraphicsItem is not QObject-based , but how i emit the signal to its parentview?
Make a new class where you inherit from graphics item and QObject
And if you have Qt 4.6.x, have a look at QGraphicsObject
Re: How to handle QGraphicsItem mouse events in QGraphicsView
Quote:
Originally Posted by
aamer4yu
Make a new class where you inherit from graphics item and QObject
And if you have Qt 4.6.x, have a look at
QGraphicsObject
appreciate, I will do that.