PDA

View Full Version : How to enable rubber band selection in QGraphicsView even if clicked on an item?



wayfaerer
25th February 2012, 18:41
I have a QGraphicsScene/View with QGraphicsItems that accept mouse events (e.g., to move or resize them). I'd like to be able to Ctrl-click the QGraphicsView to enable the rubber band selection even if the initial click location is centered on one of these items.

I know how to check whether or not Ctrl was held down in the QGraphicsItem's mousePressEvent (using "if (event->modifiers().testFlag(Qt::ControlModifier))"), but not what to do with this condition. Perhaps the QGraphicsItem is the wrong place to test this condition?

Of course, QGraphicsView seems the logical place, but from what I recall, it automatically disables rubber band selection if the click location is centered on an item that accepts mouse clicks.

Spitfire
28th February 2012, 09:54
class MyItem : public QGraphicsItem
{
public:
MyItem() {}

protected:
void mousePressEvent( QGraphicsSceneMouseEvent* event )
{
if( event->modifiers() & Qt::CTRL ) // you know that
{
event->ignore(); // and that's all you need to know :)
}
}
};