Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]How to reset QGraphicsView/QGraphicsScene to display new image without overlapping with previous image
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]How to reset QGraphicsView/QGraphicsScene to display new image without overlapping with previous image

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 24.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adnan
    wrote on last edited by
    #1

    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();
    
        }
    

    }@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cincirin
      wrote on last edited by
      #2

      Maybe you want to "clear the scene":http://qt-project.org/doc/qt-4.8/qgraphicsscene.html#clear before adding new pixmap ?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        adnan
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cincirin
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adnan
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cincirin
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                adnan
                wrote on last edited by
                #7

                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();
                
                    }
                

                }@

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  cincirin
                  wrote on last edited by
                  #8

                  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)

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    adnan
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      cincirin
                      wrote on last edited by
                      #10

                      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());
                      @

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        adnan
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved