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
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