Fit In View a loaded image to QGraphicsView
anybody has ideas how to resize an image to fit the bounds of a qgraphicsview keeping aspect ratio... WITHOUT USING sceneRect() ? . ...
how about an example for
void QGraphicsScene::render ( QPainter * painter, const QRectF & target = QRectF(), const QRectF & source = QRectF(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio ) . . . .
Re: Fit In View a loaded image to QGraphicsView
Why don't you want to use sceneRect()? You can resize the scene to the image and then fit the scene into the view, if that is an option for you.
Re: Fit In View a loaded image to QGraphicsView
sincnarf, I don't understand... you can pass any rectangle to QGraphicsView::fitInView(). It's the first argument the function takes.
Re: Fit In View a loaded image to QGraphicsView
At last this code works.... Last question on this though... By default, pixmaps are added to the scene at the center. Am i correct? But In this code my x and y for the QRectF is always 0 and 0 respectively, so this means the added graphics item is not centered. How can I make the item centered on the view?
Code:
void MainWindow
::loadGraphicsViewParameter(QGraphicsView *graphicsView
) {
if (!fileName.isEmpty()) {
if (tempImage.isNull()) {
QMessageBox::information(this, tr
("Load Warning"), tr
("Cannot load %1.").
arg(fileName
));
return;
}
QImage image
= tempImage.
convertToFormat(QImage::Format_RGB32);
int width = graphicsView->geometry().width();
int height = graphicsView->geometry().height();
// QMessageBox::information(this, tr("WIDTH HEIGHT"), tr("Width= %1 Height= %2").arg(QString::number(width, 10), QString::number(height, 10)));
(int)viewScene->width(), (int)viewScene->height()),
Qt::KeepAspectRatio, Qt::SmoothTransformation));
graphicsView
->fitInView
(QRectF(0,
0, width, height
),
Qt::KeepAspectRatio);
graphicsView->setScene(viewScene);
graphicsView->show();
}
}
Re: Fit In View a loaded image to QGraphicsView
Thanks for your post, this is really helpful for me ^^