View Full Version : MouseMoveEvent deal between QGraphicsView and QGraphicsItem
zgulser
3rd April 2009, 13:10
Hi,
I have very similar problem regarding this subject.
I have a QGraphicsView and QGraphicsScene and some QGraphicsItem (as usual). For again to zoom upan a rubberband action I needed to reimplement mouse move event in QGraphicsView. Moreover, I also want to change the color of the item whenever mouse moves on the item. So it seemed I need to reimplement the mouse move event also in the QGraphicsItem. But I doesn't work. I also tried out eventFilter thing.
Can anyone help me?
Thanks in advance
aamer4yu
3rd April 2009, 13:51
For changing color of item when mouse is over, you dont need to capture mouse move events.
You simply need a check in the paint function -
if(option->state & QStyle::State_MouseOver)
and then color the item accordingly.
:)
zgulser
6th April 2009, 06:47
Hi,
Ok but what if I want to handle the coloring issue in the mouse move event? I see that this is a general problem and asked by some users of this forum before. Is there a way to reimplement mouse move event both in GraphicsView and GraphicsItem?
Edit: By the way it doesn't work unless I remove option->state part of the if statement. Moreover, although I remove this part, It changes the color that it must change when the program begins, but you know I want to change the color of the item when the mouse is on the item. I could provide my code if you want.
Thanks in advance
zgulser
6th April 2009, 08:53
Hi,
Does anybody has an idea on this subject?
aamer4yu
7th April 2009, 13:41
You will need to store a variable for color.
Say you have QColor m_color in your graphics item class.
Then in paint event you do the following -
paint()
{
QColor color = m_color;
if(option->state & QStyle::State_MouseOver)
color = m_color.dark();
painter->fillRect(boundingRect(),color);
}
thats it, and now when you hover over the item, it will become dark.
Remember to set QGraphicsItem::setAcceptHoverEvents to true :)
zaferaltug
31st December 2010, 11:49
Ok but what if I want to handle the coloring issue in the mouse move event? I see that this is a general problem and asked by some users of this forum before. Is there a way to reimplement mouse move event both in GraphicsView and GraphicsItem?
You must add QGraphicsView::mouseMoveEvent(event) in your reipmlementation code of mouseMoveEvent. Thus your extended QGraphicsView's mouseMoveEvent is able to act like orginal QGraphicsView's mouseMoveEvent.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.