I have a very simple QGraphicsScene dervied class of my own:
class SceneComponent : public QGraphicsScene
{
Q_OBJECT
public:
explicit SceneComponent(QObject* parent = 0);
protected:
void mousePressEvent(QGraphicsSce开发者_StackOverflow社区neMouseEvent*);
};
And mousePressEvent(QGraphicsSceneMouseEvent*)
is defined as:
void SceneComponent::mousePressEvent(QGraphicsSceneMouseEvent* event) {
Q_UNUSED(event);
std::cout<<"[Processing] MouseEvent"<<std::endl;
}
To display this QGraphicsView, this is as simple as:
QGraphicsView view(sceneComp);
view.show();
Now, when I click on the Window (for the QGraphicsView) that is displayed, I get the following output:
[Processing] MouseEvent
[Processing] MouseEvent
However, when I send a synthetic event using:
QMouseEvent* mevent = new QMouseEvent(
QMouseEvent::MouseButtonPress, QPoint(50, 50),
Qt::LeftButton, Qt::NoButton, Qt::NoModifier
);
QApplication::sendEvent(&view, mevent);
I get absolutely no output. Why is this?
On a related note, installing a eventFilter
on the QGraphicsScene yields no results at all. This is probably (in my opinion) because of the fact that a QGraphicsScene expects a QGraphicsSceneMouseEvent instead of a QMouseEvent. This props up two questions:
- Why is it so that a QGraphicsScene not accept standard QEvent and only QGraphicsSceneEvent?
- Why is any QGraphicsSceneEvent derived class not instantiable?
- Why is that I can send events to a QGraphicsScene using the sceneEvent method, but I need to specify a specific QGraphicsItem to send the event to?
From the docs for QGraphicsSceneEvent
:
When a QGraphicsView receives Qt mouse, keyboard, and drag and drop events (QMouseEvent, QKeyEvent, QDragEvent, etc.), it translates them into instances of QGraphicsSceneEvent subclasses and forwards them to the QGraphicsScene it displays. The scene then forwards the events to the relevant items.
For example, when a QGraphicsView receives a QMouseEvent of type MousePress as a response to a user click, the view sends a QGraphicsSceneMouseEvent of type GraphicsSceneMousePress to the underlying QGraphicsScene through its mousePressEvent() function. The default QGraphicsScene::mousePressEvent() implementation determines which item was clicked and forwards the event to QGraphicsItem::mousePressEvent().
QGraphicsView
uses a viewport widget to display the contents of the scene and to receive user events. Try sending your event to theQGraphicsView::viewport()
widget - it should work.- By design - see the quote, these events are probably not meant to be sent manually.
QMouseEvent
is associated with a physical action - ex. mouse click, whileQGraphicsSceneMouseEvent
is something abstract... - The question is not clear, but that quote might be helpful too. Which event in particular are you talking about? You should also keep in mind that
QGraphicsScene
hasQObject
level events, sent withQApplication::sendEvent()
, andQGraphicsItem
events, that are sent withQGraphicsScene::sendEvent()
. These two kinds work in different planes and have different purposes. Refer to the docs for more info.
精彩评论