[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 ?



  • It does work but the new image is not centered, it is is not displayed at centre but at right top position



  • After adding the pixmap to scene, try to set new scene rect
    @
    displayImage->setSceneRect(0, 0, image.width(), image.height())
    @



  • 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?



  • Hmmm, maybe a wrong graphics view transform ? Can you show us the code after you've added new pixmap on scene ?



  • 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.



  • If the image dimension is less than graphics view viewport, the graphics view remains on the old scale transformation. So if you don't fit the scene in view, show it in true size:
    @
    ui->graphicsView->setTransform(QTransform());
    @



  • Thanks a lot! everything is working fine now. Thanks again!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.