PDA

View Full Version : how to save an edited image from qgraphicsview completely in QT



ivory
11th March 2014, 15:26
I am writing codes to load in image from a file and did some edits on this image(change some pixels' value), zoomed in or zoomed out and then save the image. I tried to use qgraphicview and qgraphicsscene to save the image. However, when I tried to save the image, it always save the visible region of the scene. My purpose is to save the edited image in original resolution. Below are my codes.

Loading part:
void ImageViewer::loadFile(const QString &fileName)
{
if (!fileName.isEmpty()) {
image = new QImage(fileName);
if (image->isNull()) {
QMessageBox::information(this, tr("Image Viewer"),
tr("Cannot load %1.").arg(fileName));
return;
}
qgraphicsscene = myqgraphicsview->getScene();
qgraphicsscene->setSceneRect(image->rect());
myqgraphicsview->setScene(qgraphicsscene);
qgraphicsscene->addPixmap(QPixmap::fromImage(*image));
currentName = fileName;
}
}

Editing part:
scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0, QPen(), QBrush(Qt::SolidPattern));

Saving part:
QPixmap pixMap = QPixmap::grabWidget(myqgraphicsview);
pixMap.save(fileName);
I think the problem may be in saving part. I tried to find a method in QGraphicsScene that can extract image from it, but failed. Can someone help with that? Thank you very much!!!

aamer4yu
11th March 2014, 15:44
May be QGraphicsView::render will help you :)