PDA

View Full Version : Fit In View a loaded image to QGraphicsView



sincnarf
7th October 2007, 05:08
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 ) . . . .

wysota
7th October 2007, 09:18
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.

Bitto
7th October 2007, 16:42
sincnarf, I don't understand... you can pass any rectangle to QGraphicsView::fitInView(). It's the first argument the function takes.

sincnarf
8th October 2007, 11:14
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?


void MainWindow::loadGraphicsViewParameter(QGraphicsVie w *graphicsView) {

QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
QDir::currentPath());
if (!fileName.isEmpty()) {
QImage tempImage(fileName);
if (tempImage.isNull()) {
QMessageBox::information(this, tr("Load Warning"), tr("Cannot load %1.").arg(fileName));
return;
}
QImage image = tempImage.convertToFormat(QImage::Format_RGB32);
QPixmap pixmap = QPixmap::fromImage(image);

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)));
QGraphicsScene *viewScene = new QGraphicsScene(QRectF(0, 0, width, height), 0);
QGraphicsPixmapItem *item = viewScene->addPixmap(pixmap.scaled(QSize(
(int)viewScene->width(), (int)viewScene->height()),
Qt::KeepAspectRatio, Qt::SmoothTransformation));
graphicsView->fitInView(QRectF(0, 0, width, height),
Qt::KeepAspectRatio);
graphicsView->setScene(viewScene);
graphicsView->show();
}
}

konikaka
11th September 2014, 08:23
Thanks for your post, this is really helpful for me ^^