Hi,
QGraphicsView::RubberBandDrag is normally used to select items within the rubberband but you can use it to display a rubberband and then do the zooming yourself.
You just have to set the drag mode to rubberband and install a event filter to the view's viewport to handle the click events:
view->viewport()->installEventFilter(viewEventHandler)
view->setDragMode(QGraphicsView::RubberBandDrag)
view->viewport()->installEventFilter(viewEventHandler)
To copy to clipboard, switch view to plain text mode
The view event handler than gets called and can do the zooming:
{
bool filterEvent = false;
switch(event->type()) {
case QEvent::MouseButtonPress: {
QMouseEvent * mouseEvent
= static_cast<QMouseEvent
*>
(event
);
rubberBandOrigin = mouseEvent->pos();
rubberBandActive = true;
break;
}
case QEvent::MouseButtonRelease: {
if (rubberBandActive) {
QMouseEvent * mouseEvent
= static_cast<QMouseEvent
*>
(event
);
QPoint rubberBandEnd
= mouseEvent
->pos
();
QGraphicsView * view
= static_cast<QGraphicsView
*>
(o
->parent
());
QRectF zoomRectInScene
= QRectF(view
->mapToScene
(rubberBandOrigin
),
view->mapToScene(rubberBandEnd));
view->fitInView(zoomRectInScene, Qt::KeepAspectRatio);
rubberBandActive = false;
}
break;
}
}
return filterEvent;
}
bool ViewEventHandler::eventFilter(QObject *o, QEvent *event )
{
bool filterEvent = false;
switch(event->type()) {
case QEvent::MouseButtonPress:
{
QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
rubberBandOrigin = mouseEvent->pos();
rubberBandActive = true;
break;
}
case QEvent::MouseButtonRelease:
{
if (rubberBandActive) {
QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
QPoint rubberBandEnd = mouseEvent->pos();
QGraphicsView * view = static_cast<QGraphicsView *>(o->parent());
QRectF zoomRectInScene = QRectF(view->mapToScene(rubberBandOrigin),
view->mapToScene(rubberBandEnd));
view->fitInView(zoomRectInScene, Qt::KeepAspectRatio);
rubberBandActive = false;
}
break;
}
}
return filterEvent;
}
To copy to clipboard, switch view to plain text mode
Another possibility would be to use QRubberBand.
Regards
Lodorot
Bookmarks