Important: Please read the Qt Code of Conduct -

QGraphicsScene left click selects an item AND drags a rubberband.

  • I'm trying to implement MS Excel-like selection style in my QGraphicsScene. The items are selectable by left click, but if I want to select them via rubberband, I have to start the selection outside of the item's bounds. Is there any way to select the item and drag a rubber band in the same time if the left mouse button is still not released?

    My scene is custom. I tried overriding the mousePressed event but with very little luck. Then I tried selecting the items via overriding mouseReleased (so that mousePressed can be reserved for the rubber band), but due to the nature of the rest of the UI (some other widgets are updated upon selection/deselection) it produces annoying flickering, because the Pressed event deselects, and the Released one selects the item again. Any ideas?

    Edit: This is the closest I got:

    void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
        if (event->button() != Qt::LeftButton) 
            QGraphicsItem* item = itemAt(event->scenePos(), QTransform());
           if (item != nullptr && item->isSelected()) return;
               else QGraphicsScene::mousePressEvent(event);

    Now I get the rubberband as long as the item is selected, but I have to select it first with a click, and then drag the selectionbox.

Log in to reply