3

我正在 Qt 上创建一个非常简单的图像编辑器。用户可以打开一个图像(图像显示在 a 上QGraphicsView)并且他能够顺时针或逆时针旋转。然后他可以保存旋转的图像。这是我的问题。我怎样才能让旋转的图像显示在QGraphicsView和那么保存呢?

这是我打开图像文件的代码。变量图像是QPixmap类型,图像对象是QImage指针。

打开图像代码

4

2 回答 2

3

一种方法是创建 aQPixmap然后使用 aQPainter将其绘制QGraphicsScene到像素图上。

# 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')
于 2016-02-04T02:39:42.820 回答
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)- 取决于您是要旋转项目还是整个视图。

于 2016-02-04T02:25:56.183 回答