Ryan
Ryan

Reputation: 31

How to save a 3DSurface in Qt?

I am trying to save 3DSurfaces as PDF files. The surface plots are defined like this

Q3DSurface *surface = new Q3DSurface;
surface->addSeries(mySeries);
QWidget *container = QWidget::createWindowContainer(surface);

and I am using the following code to write the container widget into a PDF file.

QPrinter printer(QPrinter::HighResolution);

printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("output.pdf");

QPainter painter(&printer);

double xscale = printer.pageRect().width() / double(container->width());
double yscale = printer.pageRect().height() / double(container->height());
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().x() + printer.pageRect().width()/ 2,
                  printer.paperRect().y() + printer.pageRect().height()/2);
painter.scale(scale, scale);
painter.translate(-main->width()/ 2, -main->height()/ 2);
container->render(&painter);

This saves a PDF file with a grey patch the size of the container, but the surface itself is not copied into the file. All help is greatly appreciated.

Upvotes: 2

Views: 1134

Answers (1)

Ryan
Ryan

Reputation: 31

I found a better answer to my question. Q3DSurface inherits the renderToImage function from the QAbstract3DGraph class, which can be used as follows to render the surface into an image file:

Q3DSurface *surface = new Q3DSurface;
surface->addSeries(mySeries);

QImage image = surface->renderToImage();
image.save("output.png");

Upvotes: 1

Related Questions