开发者

QGraphicsScene not responding to synthetic events

开发者 https://www.devze.com 2023-02-12 09:12 出处:网络
I have a very simple QGraphicsScene dervied class of my own: class SceneComponent : public QGraphicsScene

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:

  1. Why is it so that a QGraphicsScene not accept standard QEvent and only QGraphicsSceneEvent?
  2. Why is any QGraphicsSceneEvent derived class not instantiable?
  3. 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().

  1. QGraphicsView uses a viewport widget to display the contents of the scene and to receive user events. Try sending your event to the QGraphicsView::viewport() widget - it should work.
  2. 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, while QGraphicsSceneMouseEvent is something abstract...
  3. 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 has QObject level events, sent with QApplication::sendEvent(), and QGraphicsItem events, that are sent with QGraphicsScene::sendEvent(). These two kinds work in different planes and have different purposes. Refer to the docs for more info.
0

精彩评论

暂无评论...
验证码 换一张
取 消