QGraphicsItem no mouse events called
Hi!
I have subclassed QGraphicsItem class in order to create my own custom item. The following are the basic reimplemented functions.
Code:
m_size
(QSize(DEFAULT_WIDTH, DEFAULT_HEIGHT
)){
setPos(p);
setAcceptHoverEvents(true);
setAcceptDrops(true);
}
QRectF QSampleItem
::boundingRect() const {
m_size.width() + RESIZE_HANDLE_WIDTH,
m_size.height() + RESIZE_HANDLE_HEIGHT);
}
{
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.
Code:
{
qDebug()<<"Inside mousePressEvent";
m_mousePressPoint = event->scenePos().toPoint();
}
{
qDebug()<<"Inside mouseMoveEvent";
}
{
m_mousePressPoint
= QPoint(-1,
-1);
}
I have reimplemented the hoverMoveEvent and it works fine.
Any idea why the mouse events are not called?
Thanks
Re: QGraphicsItem no mouse events called
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.
Re: QGraphicsItem no mouse events called
Re: QGraphicsItem no mouse events called
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.
Re: QGraphicsItem no mouse events called
Its surprising to see that there is no solution yet to this very basic problem.
Re: QGraphicsItem no mouse events called
You need to accept() the event in mousePressEvent().
Re: QGraphicsItem no mouse events called
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...
Re: QGraphicsItem no mouse events called
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?
Re: QGraphicsItem no mouse events called
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.
Re: QGraphicsItem no mouse events called
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?
Re: QGraphicsItem no mouse events called
Did you enable mouseTracking? Unless this is enabled it won't work (as far as I can say)
Re: QGraphicsItem no mouse events called
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 :
Quote:
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 ?