I have a QGraphicsView
and a QGraphicsScene
and I enabled
this->setDragMode(QGraphicsView::RubberBandDrag);
for a Rubberband selection. However, in my application it would make sense that you need to press the CTRL key and then move the mouse to start the rubberband selection. Can I accomplish this without making my own QRubberBand? If not, how can I reimplement it?
If you have say a
QMainWindow
that contains yourQGraphicsView
and Scene, one way to do this would be to overload thekeyPressEvent
andkeyReleaseEvent
methods of QMainWindow like this:This will set the selection mode to
RubberBandDrag
as long as CTRL is being pressed. When the key is released again, the drag mode is set back to the defaultNoDrag
and no selection is performed. In both cases the event is also forwarded to the QMainWindow base class implementation which may or may not be of relevance for you.