Results 1 to 4 of 4

Thread: QPrinter / QPainter error detection

  1. #1
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Question QPrinter / QPainter error detection

    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:

    Qt Code:
    1. QPrinter printer;
    2. printer.setOutputFormat(QPrinter::PdfFormat);
    3. printer.setOutputFileName(filepath);
    4. printer.setPageSize(QPrinter::A4);
    5.  
    6. // This fails if file can't be opened for write
    7. if (painter.begin(&printer) == false)
    8. return false;
    9.  
    10. // any other call return true after this, even the end()
    11.  
    12. if (painter.end() == false)
    13. return false;
    14.  
    15. // Even this returns no error
    16. if (printer.printerState() == QPrinter::Error)
    17. return false;
    To copy to clipboard, switch view to plain text mode 

    For reference, using Qt 4.8.7, testing this on Windows 7
    Last edited by Kwakkie; 18th December 2015 at 11:22.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter / QPainter error detection

    You could try using QPdfWriter with a manually opened QFile as the QIODevice.

    Cheers,
    _

  3. #3
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPrinter / QPainter error detection

    Using Qt 4.8.7 as mentioned above. I don't think QPdfWriter is an option for Qt 4.8?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter / QPainter error detection

    Oh, right, sorry, didn't see that.

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

    Cheers,
    _

Similar Threads

  1. Memory Leak with QPrinter/QPainter
    By cevou in forum Qt Programming
    Replies: 11
    Last Post: 1st June 2010, 06:24
  2. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  3. QPainter bug or QPrinter bug?
    By lni in forum Qt Programming
    Replies: 6
    Last Post: 30th March 2009, 01:48
  4. QPainter & QPrinter on linux (fedora 7)
    By wbt_ph in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2007, 15:37
  5. QPainter & QPrinter
    By munna in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2006, 14:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.