PDA

View Full Version : QPrinter / QPainter error detection



Kwakkie
18th December 2015, 11:11
Hi all

I am trying to figure out how to detect an error when printing a pdf using QPrinter and QPainter. Error detection is obvious when selecting a file that can't be opened for write as the QPainter::begin() fails. But what happens if the file opens for write, but the medium can't be flushed (not enough room). QPainter::end() returns true, as do any QPrinter calls like newpage. I'm expecting QPrinter will use a QFile internally, but there is no way to test the QFile::flush()?

Code parts:


QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(filepath);
printer.setPageSize(QPrinter::A4);

// This fails if file can't be opened for write
if (painter.begin(&printer) == false)
return false;

// any other call return true after this, even the end()

if (painter.end() == false)
return false;

// Even this returns no error
if (printer.printerState() == QPrinter::Error)
return false;


For reference, using Qt 4.8.7, testing this on Windows 7

anda_skoa
18th December 2015, 11:53
You could try using QPdfWriter with a manually opened QFile as the QIODevice.

Cheers,
_

Kwakkie
18th December 2015, 12:11
Using Qt 4.8.7 as mentioned above. I don't think QPdfWriter is an option for Qt 4.8?

anda_skoa
18th December 2015, 12:42
Oh, right, sorry, didn't see that.

Hmm, maybe use a QFileInfo on the filename and check if its size changes between pages?

Cheers,
_