PDA

View Full Version : QGrsphicsItem click and hover events



liqxpil
18th October 2010, 13:28
hi

I need to create an inherited class of QGraphicsItem that implements the mouse click and hover events in it, can anybody give a simple example of how to do this??

thanks.

Lykurg
18th October 2010, 13:51
Simple reimplement the event handlers: QGraphicsItem::mousePressEvent(), QGraphicsItem::mouseReleaseEvent() and QGraphicsItem::mouseMoveEvent(), but for the last you have to set the corresponding flag to receive these events.

Ceelaz
18th October 2010, 13:54
Here is an example:



#include <QGraphicsItem>

class Test : public QGraphicsItem
{
Q_OBJECT
public:

// Konstruktor
Test(QGraphicsItem *parent = 0) :
QGraphicsItem(parent)
{
// initialize
}

protected:
/**
* Event: Mousbutton is released ( = Clicked)
* @param event
*/
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
// do something

// call the parents's event
QGraphicsItem::mouseReleaseEvent(event);
}

/**
* Mousecursor enters Widget.
*/
void hoverEnterEvent(QGraphicsSceneHoverEvent *event){

// do something

QGraphicsItem::hoverEnterEvent(event);
}

/**
* Mousecursor leaves Widget
*/
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {

// do something

QGraphicsItem::hoverLeaveEvent(event);
}

/**
* Mousecursor is moved in Widget.
*/
void hoverMoveEvent(QGraphicsSceneHoverEvent *event){

// do something

QGraphicsItem::hoverMoveEvent(event);
}
};

liqxpil
21st October 2010, 10:51
great thanks guys :)