[SOLVED]How to reset QGraphicsView/QGraphicsScene to display new image without overlapping with previous image
-
Whenever i open a new image to display it gets overlapped on previous image, thereby showing previous as well as new image.
@void aViewer::on_actionOpen_triggered()
{
/******** Opening file dialog for selecting image to be displayed **********/
fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());fileInfo.setFile(fileName); // Storing information of image file selected if (!fileName.isEmpty()) // if file exists { image.load(fileName); // loading image in to a QImage object if (image.isNull()) // if image does not exist display error and return { QMessageBox::information(this, tr("aViewer"),tr("Cannot load %1.").arg(fileName)); return; } /******** if image exists display image *********************/ displayImage.addPixmap(QPixmap::fromImage(image)); // add image to QGraphicsScene object ui->graphicsView->setScene(&displayImage); // pass image GraphicsView widget for displaying /****** ensure that full image is visible, i.e. scaled to viewing port *********/ if(image.height()>ui->graphicsView->height()&&image.width()>ui->graphicsView->width()) { ui->graphicsView->ensureVisible (displayImage.sceneRect()); ui->graphicsView->fitInView(displayImage.sceneRect(), Qt::KeepAspectRatio); ui->graphicsView->fitInView(displayImage.itemsBoundingRect() ,Qt::KeepAspectRatio); } /******* Displaying filename as window title *******************/ setWindowTitle(fileInfo.fileName()); /******* Update Status Bar ******************/ displayStatus(); }}@
-
Maybe you want to "clear the scene":http://qt-project.org/doc/qt-4.8/qgraphicsscene.html#clear before adding new pixmap ?
-
Thanks a ton!
It worked like a charm! Their is still a small issue, if you could figure it out.
If i open a picture of dimensions 128x128 it is shown as such, but if i open a picture of size 1600x1200 first and then open the previous image of 128x128 it is almost displayed as an 64x64 image, i wonder why? -
Thanks a lot. Here is the new code:
@void aViewer::on_actionOpen_triggered()
{
/******** Opening file dialog for selecting image to be displayed **********/
fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());fileInfo.setFile(fileName); // Storing information of image file selected if (!fileName.isEmpty()) // if file exists { image.load(fileName); // loading image in to a QImage object if (image.isNull()) // if image does not exist display error and return { QMessageBox::information(this, tr("aViewer"),tr("Cannot load %1.").arg(fileName)); return; } displayImage.clear(); /******** if image exists display image *********************/ displayImage.addPixmap(QPixmap::fromImage(image)); // add image to QGraphicsScene object displayImage.setSceneRect(0, 0, image.width(), image.height()); ui->graphicsView->setScene(&displayImage); // pass image GraphicsView widget for displaying /****** ensure that full image is visible, i.e. scaled to viewing port *********/ if(image.height()>ui->graphicsView->height()&&image.width()>ui->graphicsView->width()) { ui->graphicsView->ensureVisible (displayImage.sceneRect()); ui->graphicsView->fitInView(displayImage.sceneRect(), Qt::KeepAspectRatio); ui->graphicsView->fitInView(displayImage.itemsBoundingRect() ,Qt::KeepAspectRatio); } /******* Displaying filename as window title *******************/ setWindowTitle(fileInfo.fileName()); /******* Update Status Bar ******************/ displayStatus(); }}@
-
You don't need to call "QGraphicsView::setScene":http://qt-project.org/doc/qt-4.8/qgraphicsview.html#setScene each time. Once is enough, say after you create the scene.
Also
@
ui->graphicsView->fitInView(displayImage.sceneRect(), Qt::KeepAspectRatio);
@is enough to fit the scene in view.
I think you may set the title and status bar even if the image width or image height is less than graphics view viewport width or height. (note that you use and in condition)
-
Thanks, I made these two optimizations:
@ui->graphicsView->setScene(&displayImage); // in constructor@and only used
@ui->graphicsView->fitInView(displayImage.sceneRect(), Qt::KeepAspectRatio);@to fit the scene in view. Everything still works fine, but still the previous issue persists.