2011-09-16 1 views

答えて

20

私はこれを試したことはありませんが、これはどのように行うのかです。

あなたはいくつかの方法で 次のように一つの形があり、これを行うことができます。

QGraphicsView* view = new QGraphicsView(scene,this); 
QString fileName = "file_name.png"; 
QPixmap pixMap = QPixmap::grabWidget(view); 
pixMap.save(fileName); 
//Uses Qpixmap::grabWidget function to create a pixmap and paints the QGraphicsView inside it. 

他のレンダリング機能QGraphicsSceneを使用することです::()をレンダリングする:

QImage image(fn); 
QPainter painter(&image); 
painter.setRenderHint(QPainter::Antialiasing); 
scene.render(&painter); 
image.save("file_name.png") 
+1

素晴らしいです!ありがとう。私は第二のアプローチを試みた。必要なのは、 'QImage'を初期化する必要があることだけです。 – Donotalo

25

だけ扱った後、この問題では、新しい回答を保証するのに十分な改善があります:

scene->clearSelection();             // Selections would also render to the file 
scene->setSceneRect(scene->itemsBoundingRect());       // Re-shrink the scene to it's bounding contents 
QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene 
image.fill(Qt::transparent);            // Start all pixels transparent 

QPainter painter(&image); 
scene->render(&painter); 
image.save("file_name.png"); 
6

grabWidgetは非推奨です。グラブを使用してください。 QFileDialogを使うことができます。

QString fileName= QFileDialog::getSaveFileName(this, "Save image", QCoreApplication::applicationDirPath(), "BMP Files (*.bmp);;JPEG (*.JPEG);;PNG (*.png)"); 
    if (!fileName.isNull()) 
    { 
     QPixmap pixMap = this->ui->graphicsView->grab(); 
     pixMap.save(fileName); 
    } 
+1

あなたのソリューションと* Petrucio *ソリューションをテストしました。どちらもうまくいく、ありがとう。私はもっ​​とあなたの解決策が好きです:)、短いです。 –

関連する問題