QEvent is always set accepted in event(QEevent * event) method of a scene

  • Hello,

    I am currently stuck with a problem concerning the QEvent accepted in a QGraphicsItem of a QGraphicsScene, and I don't know how to solve it without your help.

    I have a class inheriting of QGraphicsScene (Qt 4.8.0), let say myScene. In that class, I implement the method:

    @bool myScene::event(QEvent *event){
    qDebug() << event->isAccepted();
    qDebug() << QGraphicsScene::event(even);
    qDebug() << event->isAccepted();

    //Doing general treatment on my event
    return true;


    In myScene, I have a QGraphicsItem myItem which implements the method:

    @void myItem::mousePressEvent(QGraphicsSceneMouseEvent *event){
    qDebug() << "Item clicked";
    if(....) {
    } else {

    The fact is that when I click on the item, the output of the method myScene::event is always as follow:

    Item clicked

    So, when I click on the item, whaterver this one accept the event or not, I cannot know in the myScene::event() method if the event was accepted or ignored by the item, which is problematic when I want to do the general treatment on the event. Is that normal ? How can I circumvent that issue ?

    Thank you by advance for your help.


  • The most likely answer is that there's another GraphicsItem below the one that reject the event. All items at the mouse position are queried, from top to bottom, until one accepts the event. Only if all of them explicitly reject the event, it is rejected by the scene.

    I recommend debugging into Qt to see what's actually happening. Simply start with a breakpoint in your item's event handler, and look for the method QGraphicsScenePrivate::mousePressEventHandler in the call stack. This is the method that loops through all potential receivers of the event, there you should be able to see who is actually accepting the event.

  • Thank you for tour response Asperamca. The problem was not that but you gave me useful information to get a better view of the event management and to solve the problem. In fact I got a intermediate class (I did not notice, the project is quite big and I am not alone) which was inheriting myItem, which was in fact the instance and which was also working on the event.

    However, after having noticed that, I realized that my problem was very complicate to solve nicely. Indeed, I wanted to use the "accepted" parameter of a event in myscene to sort them, but I also wanted to receive all the event in myItem (mouse press, mouse move, ...). The problem is that if I don't accept the mouse press event in myItem, I do not receive after the mouse move event. That point makes the use of the "accepted" parameter useless in myscene (since all mouse press event will be always accepted, I cannot sort them on that).

    The solution I have find is quite ugly, but do the trick for now at least: when an event does not have to be treated in myScene because it is already done in myItem, I put its screen position to the value (-1, -1) and I filter the event with that value on myScene :(


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.