0

I have a user interface with:

N.1 Push button (used to upload images)

N.2 QGraphicsView (left and right)

N.1 Push button that takes a print screen of the current image loaded on QGraphicsView left

Using the mouse is possible to:

1) zoom-in/zoom-out from the image

2) draw rectangles on the image.

I want to take the print screen of the image according to the zoom-in or zoom-out area I am using. However, once the file is saved it shows the entire image (wrong because I wanted only the enlarged or shrank part) with the rectangles drawn (this is correct).

According to this post QFileDialog was used in a similar way I am trying to do. I successfully used QFileDialog::getSaveFileName() to save the image. However it is not entirely solving the problem.

Below the pushbutton that takes care of taking the print screen of the image in the QGraphicsView left:

void MainWindow::on_addNewRecordBtn_clicked()
{
    leftScene->clearSelection();     // Selections would also render to the file
    leftScene->setSceneRect(leftScene->itemsBoundingRect());    // Re-shrink the scene to it's bounding contents
    QImage image(leftScene->sceneRect().size().toSize(), QImage::Format_ARGB32);  // Create the image with the exact size of the shrunk scene
    image.fill(Qt::transparent);     // Start all pixels transparent
    QPainter painter(&image);
    leftScene->render(&painter);
    image.save(QFileDialog::getSaveFileName(this, tr("New Image Name"), QDir::rootPath(),
                                        "Name (*.jpg *.jpeg *.png *.tiff *.tif)"));
}

The expected result would be saving the zoomed image (zoom.jpg for example) like this:

zoomed Particular

However when I save the image (zoom.jpg) the result that I am obtaining is constantly the entire image with the drawn features:

entire image

Emanuele
  • 2,194
  • 6
  • 32
  • 71
  • hi eyllanesc, you can download a small minimal, complete and verifiable application I prepared from here: `git clone https://ERaggi@bitbucket.org/ERaggi/savescaledimg.git` You will see that if you save the zoomed (or shrank) image on, for example, your desktop with different names, you will see the saved image always of the same size despite the print screen was performed zooming in first and zooming out after. – Emanuele Jan 15 '19 at 05:40

1 Answers1

0

So if anyone needs, it is possible to take a print screen of an image no matter what the zoom in. Meaning you can zoom-in and zoom-out and take a print screen. The following statement will do the job, grabbing the image your present (zoomed in or out status):

QImage image = ui->leftView->grab().toImage();

The only glitch is that the scroll bars horizontal and vertical (depending on the zoom) are also printed in your image. You can avoid that by setting them off right before taking the print screen and putting them back on right after. Basically my previous function can be better written as follows:

void MainWindow::on_addNewRecordBtn_clicked()
{
    leftScene->setSceneRect(leftScene->itemsBoundingRect());
    // Setting off the scroll bars
    ui->leftView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->leftView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QImage image = ui->leftView->grab().toImage();
    image.save(QFileDialog::getSaveFileName(this, tr("New Image Name"), QDir::rootPath(),
                                            "Name (*.jpg *.jpeg *.png *.tiff *.tif)"));
    // Putting the scroll bars back on
    ui->leftView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    ui->leftView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
}

Hope this will be helpful in case you encountered my same problem.

Emanuele
  • 2,194
  • 6
  • 32
  • 71