2016-02-04 21 views
1

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

这里是我打开图像文件的代码。可变图像是QPixmap类型,imageObject是QImage指针。

Opening Image Code

+1

而不是使用您的代码的图像,为什么不只是发布代码在您的问题。 –

回答

0

this method请看:

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

呈现源rect,这是在视图坐标,从场景 到目标,这是在涂料设备坐标,使用画家。此 功能是用于捕获视图的内容到一个涂料 装置,例如一个QImage的(例如,获取屏幕截图)

至于旋转是有用的,你需要的方法是void QGraphicsItem::setRotation(qreal angle)或备选地void QGraphicsView::rotate(qreal angle) - 取决于您是要旋转项目还是整个视图。

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') 
+0

非常感谢你在最后的工作中非常有帮助! @BrendanAbel –