是應用了他的render函數,render的作用是:
Renders the source rect from scene into target, using painter. This function is useful for capturing the contents of the scene onto a paint device, such as a QImage (e.g., to take a screenshot), or for printing with QPrinter. For
If source is a null rect, this function will use sceneRect() to determine what to render. If target is a null rect, the dimensions of painter's paint device will be used.
The source rect contents will be transformed according to aspectRatioMode to fit into the target rect. By default, the aspect ratio is kept, and source is scaled to fit in target.
代碼如下:
//get thumbnail
QImage image(130 * mSceneSize.width()/ mSceneSize.height(),130 ,QImage::Format_ARGB32);
QString pngName = currentPageID+"_scene.png";
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
scene->render(&painter);
bool saveSuccess = image.save(pngName);
Q_ASSERT(saveSuccess == true);
有兩個注意事項:
- 如果你把QImage image(130 * mSceneSize.width()/ mSceneSize.height(),130 ,QImage::Format_ARGB32); 寫成QImage image。無論如何image都是無法save成功的。因爲image沒有初始化。
- 如果你的scene裏有的item/widget的座標位於目前可顯示的外面,意思是你的scene現在大小是100*100,但是有一個item的座標位於100*101,那麼調用scene->render時會有ASSERT錯誤:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Error!
Program: ...\Win32\Debug\Maker.exe
Module: 5.0.1
File: global\qglobal.cpp
Line: 1951
ASSERT: "!item->d_ptr->itemDiscovered" in file graphicsview\qgraphicsscenebsptreeindex.cpp, line 343
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
|
你當然可以忽略掉他,在release模式下是沒有此類問題的。
當然了你用QGraphicsView的render方法也可以獲得view的縮略圖。