2

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.

Ryan
  • 31
  • 5
  • Try making a pixmap out of QGuiApplication::screens ()[0]->grabWindow (container->winId (), 0,0, container->width (), container->height ()); and putting it on a label or something – Mike Jun 25 '18 at 21:16
  • Instead of the last block in my original post, I now have QPixmap pmap = QGuiApplication::screens()[0]->grabWindow(container->winId(), 0, 0, container->width(), contianer->height()); pmap.save("output.pdf"); This takes a screenshot of the IDE in the same spot as my container appears, but it does not capture my container. – Ryan Jun 25 '18 at 21:34
  • Then use the winId of the parent – Mike Jun 26 '18 at 03:26
  • Not quite sure if what im suggesting is a hack or not. I guess i can try throwing together a pdfgen.exe tomorrow n give the samples of the core. Ive never used 3dsurface before in my experiance qt has a lot of great convenience class built in but sometimes "dumbing" down the objective and getting from a to b before percecting is the better approach. – Mike Jun 26 '18 at 03:32
  • I think that for whatever reason, the program is not rendering the container window before grabWindow is called. I worked around this by adding a button which saves the container when pressed. This allows the image to be rendered on the screen first, then get captured by grabWindow. It's not the most elegant solution, but it works. Thank you for your help! – Ryan Jun 26 '18 at 14:30

1 Answers1

1

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");
Ryan
  • 31
  • 5