c++ - how to save an edited image from qgraphicsview in QT -
i writing codes load in image file , did edits on image(change pixels' value), zoomed in or zoomed out , save image. tried use qgraphicview , qgraphicsscene save image. however, when tried save image, save visible region of scene. purpose save edited image in original resolution. below 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; } } saving part:
qpixmap pixmap = qpixmap::grabwidget(myqgraphicsview); pixmap.save(filename); i think problem may in saving part. tried find method in qgraphicsscene can extract image it, failed. can that? thank much!!!
the addpixmap method returns qgraphicspixmapitem*. need remember it, , use save pixmap. example:
class imageviewer { qgraphicsview * m_view; qstring m_currentname; qgraphicspixmapitem * m_item; ... }; imageviewer::imageviewer() : m_item(nullptr) { ... } void imageviewer::loadfile(const qstring &filename) { if (filename.isempty()) return; qimage image(filename); if (image.isnull()) { qmessagebox::information(this, tr("image viewer"), tr("cannot load %1.").arg(filename)); return; } qgraphicsscene * scene = m_view->scene(); scene->setscenerect(image->rect()); m_item = scene->addpixmap(qpixmap::fromimage(image)); m_currentname = filename; } void imageviewer::save() { if (!m_item) return; qsavefile file(m_currentname); if (! file.open(qiodevice::writeonly | qiodevice::unbuffered)) { qmessagebox::information(this, tr("image viewer"), tr("cannot save %1.").arg(m_currentname)); return; } qimage const image = m_item->pixmap().toimage(); image.save(&file); file.commit(); }
Comments
Post a Comment