Select multiple QGraphicsItems without holding down key

670 Views Asked by At

I'm using a QGraphincsView that holds several elements which inherit from QGraphicsItem. The whole thing works fine, I can select them as desired. And when I hold down the Ctrl-key I can select several of them.

Now I want to implement an optional multi-selection without the need to hold down Ctrl-key. I already tried to set the related modifier in mouse-press-event by calling

evt->setModifiers(Qt::ControlModifier);

before the event is handed over to it's base-class QGraphicsItem but this does not work.

So my question: what has to be done to get multiple selection functionality by default and without holding down a key?

Thanks!

1

There are 1 best solutions below

0
On

This is controlled by the QGraphicsScene. You stated: -

I'm using a QGraphincsView that holds several elements which inherit from QGraphicsItem

This is not actually the case. A QGraphicsView is a window into an area of the scene; it is a QGraphicsScene which holds items derived from QGraphicsItem.

You can see in the documentation that the QGraphicsScene has functions such as selectedItems(), selectionArea() and setSelectionArea(). While a QGraphicsItem can be selected with QGraphicsItem::setSelected, the control of what happens when you click an item is governed by the QGraphicsScene, with the event having been passed from the QGraphicsView.

If you inherit from QGraphicsScene, you can override the mouse methods; mousePressEvent, mouseMoveEvent, mouseReleaseEvent. This will allow you to monitor when the user selects consecutive items by clicking on them and react by calling their QGraphicsItem::setSelected function.

Alternatively, depending upon your design, you can allow the user to draw an area on the scene and call QGraphicsScene::setSelectionArea, which will set all the items surrounded by the given QPainterPath.