PDA

View Full Version : QGraphicsItem no mouse events called



munna
25th May 2009, 12:31
Hi!

I have subclassed QGraphicsItem class in order to create my own custom item. The following are the basic reimplemented functions.



QSampleItem::QSampleItem(QPointF p, QGraphicsItem *parent)
: QGraphicsItem(parent),
m_size(QSize(DEFAULT_WIDTH, DEFAULT_HEIGHT))
{
setPos(p);

setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemIsMovable, true);
setAcceptHoverEvents(true);
setAcceptDrops(true);
}

QRectF QSampleItem::boundingRect() const
{
return QRectF(0, 0,
m_size.width() + RESIZE_HANDLE_WIDTH,
m_size.height() + RESIZE_HANDLE_HEIGHT);
}

void QSampleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);

painter->drawRoundedRect(RESIZE_HANDLE_WIDTH/2,
RESIZE_HANDLE_HEIGHT/2,
m_size.width(),
m_size.height(),
5, 5);
}


I have not re-implemented QGraphicsScene's mouse event functions. In the class above I need to reimplement the mouse event functions so that I can resize the shape from different directions and also move around.

But the none of following mouse event functions are called in my class.



void QSampleItem::mousePressEvent(QGraphicsSceneMouseEv ent *event)
{
qDebug()<<"Inside mousePressEvent";
m_mousePressPoint = event->scenePos().toPoint();

QGraphicsItem::mousePressEvent(event);
}

void QSampleItem::mouseMoveEvent(QGraphicsSceneMouseEve nt *event)
{
qDebug()<<"Inside mouseMoveEvent";

QGraphicsItem::mouseMoveEvent(event);
}

void QSampleItem::mouseReleaseEvent(QGraphicsSceneMouse Event *event)
{
m_mousePressPoint = QPoint(-1, -1);

QGraphicsItem::mouseReleaseEvent(event);
}


I have reimplemented the hoverMoveEvent and it works fine.

Any idea why the mouse events are not called?

Thanks

zgulser
25th May 2009, 12:44
Hi,

I had a similar problem a little time ago. Yes you are right. mouseMoveEvent(..) isn't called at all.( But mousePressEvent should have been called by the way!)

There are lots of messages regarding this issue in the forum. But if you only need some cosmetic changes, I can suggest you to use QStyle.

Regards.

munna
25th May 2009, 13:07
So is this a bug?

zgulser
25th May 2009, 13:24
I am not expert mate. Maybe there is a particular configuration or setting you need to do. But I tried about 2 weeks to do what you're trying to do. I have also posted many messages regarding this issue but I couldn't acquire the solution of mine.

munna
25th May 2009, 14:18
Its surprising to see that there is no solution yet to this very basic problem.

jpn
25th May 2009, 15:57
You need to accept() the event in mousePressEvent().

zgulser
4th June 2009, 10:22
Hi,

That means I need to click at least to accept move events? It sounds like kind of ridiculous. Nevertheless, I tried it but it didn't work...

jpn
4th June 2009, 10:51
No, I mean that in order to receive mouse move events, you must inform the framework that you are interested in receiving them. This is done by accepting the mouse press event. Notice that the default implementation of QGraphicsItem::mousePressEvent() ignores the event. There is a very valid reason to do this. Why deliver tons of move events if the item is not really interested receiving them?

Bryku
8th December 2009, 18:52
Hi.
Yes, but what if i just want to recieve events when my mouse coursor is over my item. I don't want to press mouse button to check if my coursor is over my item.

Regards.

advokate3
8th December 2009, 20:25
From what I understand, calling event.accept() cancels the "propagation" of the events up the view tree. What if you want to "listen" to the event on your current object but still want the event to keep bubbling up the view hiearchy? is there a way to accomplish that?

soxs060389
9th December 2009, 14:55
Did you enable mouseTracking? Unless this is enabled it won't work (as far as I can say)

Bryku
9th December 2009, 15:43
Hmm, no I didn't, but i can't find any methods or properity , that enables mouse tracking. Besides , this probably won't work , becouse :


If you do receive this event, you can be certain that this item also received a mouse press event,

So you have to push the button in order to recieve mouse move events. But I don't want that.As I said, I only need to know when my mouse coursor is over my graphicsItem, then emit some signals. Is there any way to accomplish that ?