Assuming that you have a rect you want to display (from mouse press/release events) then simplest thing to do is to use:
QGraphicsView::fitInView ( const QRectF
& rect, Qt
::AspectRatioMode aspectRatioMode
= Qt
::IgnoreAspectRatio )
QGraphicsView::fitInView ( const QRectF& rect, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio )
To copy to clipboard, switch view to plain text mode
You can use Qt::KeepAspectRatio if that's what you want.
Also when getting top-left and bottom-right points for the rectangle you should map them to sceene coordinates:
{
selectedRect.setTopLeft(this->mapToScene(e->pos()));
}
{
selectedRect.setBottomRight(this->mapToScene(e->pos()));
this->fitInView(selectedRect, Qt::KeepAspectRatio);
}
void ZoomableView::mousePressEvent( QMouseEvent* e )
{
selectedRect.setTopLeft(this->mapToScene(e->pos()));
}
void ZoomableView::mouseReleaseEvent( QMouseEvent* e )
{
selectedRect.setBottomRight(this->mapToScene(e->pos()));
this->fitInView(selectedRect, Qt::KeepAspectRatio);
}
To copy to clipboard, switch view to plain text mode
You can also disable the scroll bars if you don't want them:
QGraphicsView::setHorizontalScrollBarPolicy(Qt
::ScrollBarAlwaysOff);
QGraphicsView::setVerticalScrollBarPolicy(Qt
::ScrollBarAlwaysOff);
QGraphicsView::setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QGraphicsView::setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
To copy to clipboard, switch view to plain text mode
Bookmarks