如何从QGraphicsView保存图像?

时间:2016-02-04 02:19:06

标签: c++ image qt qgraphicsview image-editor

我在Qt上创建了一个非常简单的图像编辑器。用户可以打开图像(图像显示在QGraphicsView上),他可以顺时针或逆时针旋转。然后他可以保存旋转的图像。这就是我的问题。我怎么能得到旋转图像显示在QGraphicsView中,然后保存?

以下是打开图像文件的代码。变量图像为QPixmap类型,imageObject为QImage指针。

Opening Image Code

2 个答案:

答案 0 :(得分:1)

执行此操作的一种方法是创建QPixmap,然后使用QPainterQGraphicsScene绘制到像素图上。

# 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')

答案 1 :(得分:0)

看看this method

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

从场景中渲染视图坐标中的源矩形   使用画家进入绘画设备坐标中的目标。这个   函数用于将视图内容捕获到绘制上   设备,例如QImage(例如,截取屏幕截图)

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