2016-02-04 28 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 –