Important: Please read the Qt Code of Conduct -

QGraphicsScene selected Items count is returned wrong in MousePress/MouseRelease event.

  • I have captured the mousePress and Mouserelease Event in the QGraphicsScene. I have drawn multiple QgraphicsItems (Box, Line etc) on the scene. When I select a particular graphicsItems using mouse and get the count of the selected items using "selectedItems().count()", it properly returns 1. But when I select multiple items using Ctrl key on keyboard and mouse the count always shows 1 and returns the list of QgraphicsItem which was last selected.

    How can i get the actual value of the selected items when multiple are selected ?

  • Sorry it was my mistake.
    I was getting the count of the selected items before passing on the "QGraphicsScene::mouseReleaseEvent(mouseEvent);" further to the underlying items on the scene.

    So before calling the function to get the count of the selected items be sure you pass on the mouseReleaseEvent further so that the QGraphicsItem can catch and do the processing needed to make it selected.

    in short

    void ImageGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)

    // selectedItems().count() - Wrong

    QGraphicsScene::mouseReleaseEvent(mouseEvent); // Here the QGraphicsItem flag for selection is set.

    // selectedItems().count() - Correct

Log in to reply