【问题标题】:How to save images from QGraphicsView?如何从 QGraphicsView 保存图像?
【发布时间】:2016-02-04 02:19:06
【问题描述】:

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

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

【问题讨论】:

  • 与其使用代码图像,不如直接在问题中发布代码。

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


【解决方案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')

【讨论】:

  • 非常感谢您的帮助,终于成功了! @BrendanAbel
【解决方案2】:

看看this method

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) - 取决于您是要旋转项目还是整个视图。

【讨论】:

    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2021-08-26
    相关资源
    最近更新 更多