Hi,
I have some QGraphicsItems on my scene. I'am percepting mouse events in the QGraphicsView class. Once I try to reimplement mouseMoveEvent in one of my QGraphicsItem, it does't do anything.
Any ideas?
Regards.
Hi,
I have some QGraphicsItems on my scene. I'am percepting mouse events in the QGraphicsView class. Once I try to reimplement mouseMoveEvent in one of my QGraphicsItem, it does't do anything.
Any ideas?
Regards.
Hi,
take a look at QGraphicsItem::setAcceptHoverEvents
Well, I don't wanna percept hover actually.
I want to percept mouse move or press when the cursor is moved over the item for example.
Regards.
In order for a graphics item to receive mouse move events, it must first accept the mouse press event.
J-P Nurmi
Hi again,
I reimplemented mouseMoveEvent and mousePressEvent in the QGraphicsView class. From QGraphicsView::mousePressEvent(), I can handle which item is pressed by calling ...items(QPointF). But I want to handle it from the QGraphicsItem class. To do this I reimplemented mousePressEvent() again in the QGraphicsItem class but I couldn't get any respond upon any mouse presses although I first reimplemented mousePressEvent.
Regards
Last edited by zgulser; 10th February 2009 at 13:34.
Why? QGraphicsView is supposed to forward mouse events to the scene, which in turn delivers them to those items that they belong to. Custom mouse event handlers in the view tend to break the graphics view framework event handling unless you really now what you're doing.
The default implementation of QGraphicsScene already does this in an efficient way.From QGraphicsView::mousePressEvent(), I can handle which item is pressed by calling ...items(QPointF).
The default implementation of QGraphicsScene delivers mouse events to those items that they belong to.But I want to handle it from the QGraphicsItem class.
Probably because the QGraphicsView::mousePressEvent() reimplementation breaks the event delivery chain. Make sure you call the base class implementation.To do this I reimplement mousePressEvent() again in the QGraphicsItem class but I couldn't get any respond upon any mouse presses although I first reipmlement mousePressEvent.
J-P Nurmi
I have a QGraphicsScene object inside my QGraphicsView. So that's why I reimplemented mouse events in QGraphicsView. I did the following
void QGraphicsView::mousePressEvent(QMouseEvent* mouseEvent)
{
QPointF p = mapToScene(mouseEvent.x(), mouseEvent.y());
itemsList = this->scene.items(p);
...
}
Ok.The default implementation of QGraphicsScene already does this in an efficient way.
How I am handling events in items? By reimplementing mouse events in QGraphicsItems?The default implementation of QGraphicsScene delivers mouse events to those items that they belong to.
What do you mean by base class implementation?Probably because the QGraphicsView::mousePressEvent() reimplementation breaks the event delivery chain. Make sure you call the base class implementation.
Thanks.
I don't understand what you mean. What are you trying to achieve with that implementation?
Yes.How I am handling events in items? By reimplementing mouse events in QGraphicsItems?
I meant something like this:What do you mean by base class implementation?
But looking at your current implementation, I'm not convinced at all you should even need to reimplement QGraphicsView::mousePressEvent().Qt Code:
void SubClass::function() { BaseClass::function(); // <-- call the base class implementation // ... do your own things }To copy to clipboard, switch view to plain text mode
J-P Nurmi
Thanks JPN for your usefull answers.
What I did is something different. Instead of reimplementing mouse events inside QGraphicsScene, I reimplemented those in QGraphicsView and then used mapToScene in order to achieve convenience. By doing so, I figured out that mouse events are also delivered to the corresponding items.
By the way, I just missed to write QGraphicsView::mousePressEvent() inside the QGraphicsView::mousePressEvent() { ... }. That's why it didn't work until now.
King Regards
First of all
I wonder what exactly does the statement in the following mean? This is from the documentation.
"QGraphicsScene's event propagation architecture schedules scene events for delivery to items, and also manages propagation between items. If the scene receives a mouse press event at a certain position, the scene passes the event on to whichever item is at that position."
Does it mean that I should handle mouse events first in QGraphicsScene and then in QGraphicsItem class?
Secondly,
I guess you mean QGraphicsView by first place. If it is, here is my reason;
I have a view which has a QGraphicsScene object in it. So I do not create any class from QGraphicsScene. If I created, be sure that I did the way you told above as normally which might be a better way. I'am totally open for such development issues with no argue
By the way, what's wrong with handling mouse events in the QGraphicsView?
Regards.
Last edited by zgulser; 10th February 2009 at 20:25.
Bookmarks