PDA

View Full Version : Printing WebEngine Pages



brixel
9th May 2016, 18:16
Hello,

So it would appear that in Qt 5.6 printing for the web engine isn't as straight forward as I had hoped. I've not yet been able to locate an example of how this should be accomplished and was looking to see if anyone here could provide an example.

Basically I'd like to invoke a preview of the page about to be presented so the end user can make adjustments, choose different printers, etc. and then print the page that is displayed in the current view of the web engine.

I was able to get something to print using the following ( based on a few pieced together examples ), but it was very tiny on the page.



QPrinter printer(QPrinter::HighResolution);
QPainter painter;
painter.begin(&printer);
webEngineView->render(&painter);
painter.end();


I didn't see anything obvious like a print function off the web view ( though that would be really nice ). I also did look at the QPrintPreviewDialogue, but wasn't sure how to incorporate it here.

So if anyone has implemented printing in 5.6 for the web engine view as described above, an example would be helpful. Thanks.

anda_skoa
10th May 2016, 06:56
Print preview would work similar: basically if you put your lines 3-5 into a slot and call it from the place you have right now, you can also connect that slot to the print request signal of the preview dialog.

Cheers,
_

brixel
10th May 2016, 12:03
Ok, so I put the following together based on you advice:



void MainWindow::print(QPrinter* printer)
{
QPainter painter;
painter.begin(printer);
webEngineView->render(&painter);
painter.end();
}




void MainWindow::printPreview()
{
QPrinter printer;
QPrintPreviewDialog preview(&printer, this);
connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(print(QPrinter*)));
preview.exec();
}


When I call printPreview I do get the preview dialogue now and I can see the page ( though it is tiny, but I can work with the scaling issue later ), however after about 2 or 3 seconds the program crashes. Anything obvious I'm missing here?

Added after 21 minutes:

Never mind. This does work as expected, there was another, unrelated problem causing the crash.