Do you want a once-only resize, or do you want the window to stay fixed at this new size? That is, do you want the user to be able to resize the window after your resize?
Most of the time, when you want the viewport to assume a size you can rely on QGraphicsView's sizeHint(), which will default to the size of the scene. Otherwise, you can easily determine the size (bounding rect) of any item on the scene by going
void CustomView
::keyPressEvent(QKeyEvent *event
) {
// whenever someone hits a key, resize the view to an item's size.
QRect itemRect
= mapFromScene
(pixmap
->boundingRect
()).
boundingRect().
toRect();
resize
(itemRect.
size() + QSize(frameWidth
() * 2, frameWidth
() * 2);
}
void CustomView::keyPressEvent(QKeyEvent *event)
{
// whenever someone hits a key, resize the view to an item's size.
QRect itemRect = mapFromScene(pixmap->boundingRect()).boundingRect().toRect();
resize(itemRect.size() + QSize(frameWidth() * 2, frameWidth() * 2);
}
To copy to clipboard, switch view to plain text mode
The pixmap boundingrect is a bit larger than the pixmap itself; depending on how you display the pixmap you could map just a QRectF(QPointF(), pixmapSize) instead.
You can also go the opposite way; call QGraphicsView::fitInView() to make the item fit inside the view without actually resizing the view. Keep in mind that this approach uses transformations, though, slow on X11.
Bookmarks