PDA

View Full Version : How to handle QGraphicsItem mouse events in QGraphicsView



HiJack
27th April 2010, 04:21
hi all,
I wrote some objects derived from QGraphicsView and QGraphicsItem, and I simulate a mouse click event in my QGraphicsItem just like this


void QMyItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
{
// if(e->buttons() == Qt::LeftButton)
// SetButtonState(NORMAL);

QGraphicsPixmapItem::mousePressEvent(event);

setSelected(false); // i don't need the focus

QPointF lastpt = event->lastPos();
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,

void QMyGraphicsView::Command( param)
{
switch(param)
{
case ?? : ...
case ?? : ...
}
...
}

or is there a better place to dispose other than QGraphicsView?

thanks for any advice.

aamer4yu
27th April 2010, 05:32
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.

Archimedes
27th April 2010, 05:52
One good place to do it is in the QGraphicsScene, QGraphicsScene::mouseGrabberItem() (http://doc.qt.nokia.com/4.6/qgraphicsscene.html#mouseGrabberItem) will return the item that accepted a mousePressEvent (at the release it will not return it though) .

Another one is QGraphicsScene::focusItem() (http://doc.qt.nokia.com/4.6/qgraphicsscene.html#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) (http://doc.qt.nokia.com/4.6/qgraphicsview.html#itemAt).

You can do the same in QGraphicsScene with QGraphicsScene::itemAt(...) (http://doc.qt.nokia.com/4.6/qgraphicsscene.html#itemAt) (will return the topmost visible item at that position) or QGraphicsScene::items(...) (http://doc.qt.nokia.com/4.6/qgraphicsscene.html#items-3) (you'll get all the items at that position)

HiJack
27th April 2010, 07:08
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.

aamer4yu
27th April 2010, 10:36
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

HiJack
27th April 2010, 11:04
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.