PDA

View Full Version : mouseMoveEvent in QGraphicsItem



navi1084
6th July 2009, 11:50
Dear All,
I have re-implemented mouseMoveEvent(QGraphicsSceneMouseEvent *event) in my class which is derived from QGraphicsItem. When i draw an item on scene and move the mouse cursor inside the item, mouseMoveEvent() will not trigger. It will only trigger when i click on the item and move the mouse inside the item (i.e leftclick + move). I have set the setAcceptHoverEvents(true). But still mouseMoveEvent() event doesn't trigger. Can anyone tell how can i resolve this problem such that, mouse move on item trigger mouseMoveEvent(). Following is my code


CMyItem::CMyItem(QPointF center, QSizeF size)
{
m_Rectangle.setSize(size);
m_Rectangle.moveTopLeft(center);
setZValue(((int)m_Rectangle.top() + (int)m_Rectangle.left())% 2);

setFlags(ItemIsSelectable | ItemIsMovable);
setAcceptHoverEvents(true);
}

CMyItem::~CMyItem()
{

}

QRectF CMyItem::boundingRect() const
{
return QRectF(0, 0, 200, 100);
}

QPainterPath CMyItem::shape() const
{
QPainterPath path;
path.addRect(0, 0, 200, 100);
return path;
}

void CMyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
//QBrush brush(
QPen pen;
pen.setColor(Qt::black);
painter->setPen(pen);
painter->drawRect(m_Rectangle);
}

void CMyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mousePressEvent(event);
update();
event->accept();

}

void CMyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mouseMoveEvent(event);
}

void CMyItem::mouseReleaseEvent(QGraphicsSceneMouseEven t *event)
{
QGraphicsItem::mouseReleaseEvent(event);
update();
}

navi1084
6th July 2009, 12:32
Any answer for this please????

wysota
6th July 2009, 12:33
Enable mouse tracking for the view's viewport.

zgulser
6th July 2009, 13:06
Hi,

this problem was discussed couple of times before in the forum. For example the following,

http://www.qtcentre.org/forum/f-qt-programming-2/t-qgraphicsitem-no-mouse-events-called-21256.html/?highlight=mouseMoveEvent

but I still got the same problem with it. So good luck:)

navi1084
6th July 2009, 13:17
i have already used setMouseTracking() for view. but still its not working. In Qt Asistant i have checked description of setAcceptHoverEvents(). i.e.
Hover events are commonly used to highlight an item when it's entered, and for tracking the mouse cursor as it hovers over the item (equivalent to QWidget::mouseTracking).

But setAcceptHoverEvents() doesn't act exactly as setMouseTracking()

wysota
6th July 2009, 13:34
But why do you expect a hover event to trigger a mouse move event? It will trigger hoverMoveEvent()...

navi1084
6th July 2009, 13:41
you are absolutely correct wysota. thank you very much. then i have misunderstood the QGraphicsItem mouseMoveEvent() with QWidget mouseMoveEvent(). Because mouseMoveEvent() itself will be trigger when user hover the mouse on widget.
My problem is solved. Thank you very much.