如何从QGraphicsView保存图像?

3
我正在Qt上创建一个非常简单的图像编辑器。用户可以打开图像(该图像显示在QGraphicsView上),并具有顺时针或逆时针旋转的能力。然后,他可以保存旋转后的图像。我的问题出在这里。我如何获取旋转后的图像在QGraphicsView中显示,然后保存它?
以下是我打开图像文件的代码。变量image是QPixmap类型,imageObject是一个QImage指针:

Opening Image Code


1
为什么不直接在你的问题中发布代码,而非使用代码截图呢? - Brendan Abel
2个回答

3

其中一种方法是创建一个QPixmap,然后使用QPainterQGraphicsScene绘制到这个pixmap上。

# Get the size of your graphicsview
rect = graphicsview.viewport().rect()

# Create a pixmap the same size as your graphicsview
# You can make this larger or smaller if you want.
pixmap = QPixmap(rect.size())
painter = QPainter(pixmap)

# Render the graphicsview onto the pixmap and save it out.
graphicsview.render(painter, pixmap.rect(), rect)
pixmap.save('/path/to/file.png')

非常感谢您的帮助,最终它成功了!@BrendanAbel - Panagiotis Sakkis

0

看一下这个方法

void QGraphicsView::render(QPainter * painter, const QRectF & target = QRectF(), const QRect & source = QRect(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio)

将位于视图坐标中的源矩形从场景渲染到以绘制设备坐标为基础的目标上,使用painter。此函数可用于将视图内容捕获到绘制设备(例如QImage)上,例如截屏。

至于旋转,您需要的方法是void QGraphicsItem::setRotation(qreal angle)或者void QGraphicsView::rotate(qreal angle) - 具体取决于您是要旋转项目还是整个视图。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接