When printing to PDF with QTextDocument and QPrinter is there any way of detecting errors (e.g. not being able to write to the PDF file)? I'm using the following code:
QTextDocument document;
QPrinter printer( QPrinter::HighResolution );
printer.setOutputFormat( QPrinter::PdfFormat );
printer.setOutputFileName( filename );
document.print( &printer );
In the docs you'll find QPrinter::printerState. So you can definitely do:
if (printer.printerState() == QPrinter::Error)
// do some error handling
I admit that's not a lot work with, as there are only 4 QPrinter::PrinterState's. You might want to do your best to avoid errors in the first place. The detailed description in the doc states:
Note that setting parameters like paper size and resolution on an invalid printer is undefined. You can use
QPrinter::isValid()to verify this before changing any parameters.
Additionaly, you could check if the filename you are setting already exists using QFile::exists.
Also, when setting it all up you can call and handle QPrinter::supportedResolutions(), QPrinter::supportedPaperSources() and QPrinter::supportsMultipleCopies(). Of course, printing to PDF you might not have to worry about these.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With