Results 1 to 2 of 2

Thread: RichText printing with HighResolution printer

  1. #1
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default RichText printing with HighResolution printer

    Hi there,

    I have trouble printing a rich text document to a printer with HighResolution settings.

    So far I use something in the lines of:
    Qt Code:
    1. QAbstractTextDocumentLayout* layout = text.documentLayout();
    2.  
    3. QAbstractTextDocumentLayout::PaintContext context;
    4. context.palette.setColor(QPalette::Text, painter->pen().color());
    5. layout->draw(painter, context);
    To copy to clipboard, switch view to plain text mode 
    whereas text is a QTextDocument, and painter is a QPainter created for a printer object. The printer was initialized with QPrinter::HighResolution.

    It seems that printing with high-resolution is somewhat tricky. For example, the font metrics returned have to be scaled manually to obtain a correct line spacing. And maybe that's also the reason why the draw() function of QAbstractTextDocumentLayout doesn't work as expected?

    Any suggestions are welcome!
    Andreas
    Andreas

  2. #2
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Lightbulb Re: RichText printing with HighResolution printer

    Heureka!

    The Qt-Documentation is somewhat sparse at this point. In any case when printing with HighResolution you always have to specify the QPaintDevice before drawing. This applies to QFontMetrics where you need to instantiate the instance with

    Qt Code:
    1. QFontMetrics fm(myFont, p->device()); // p = QPainter *
    To copy to clipboard, switch view to plain text mode 

    Also, for the rich-text printing, you need to set the QPaintDevice before drawing. The code snipped below shows how it works:

    Qt Code:
    1. QAbstractTextDocumentLayout* layout = text.documentLayout();
    2.  
    3. layout->setPaintDevice(painter->device()); // this is needed for HighRes printing
    4.  
    5. QAbstractTextDocumentLayout::PaintContext context;
    6. context.palette.setColor(QPalette::Text, painter->pen().color());
    7. layout->draw(painter, context);
    To copy to clipboard, switch view to plain text mode 
    Andreas

Similar Threads

  1. Replies: 6
    Last Post: 22nd November 2016, 13:57
  2. font Kerning while printing on printer in 4.4 beta1
    By vladeck in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2008, 00:27
  3. Printing problem in windows
    By joseph in forum Qt Programming
    Replies: 6
    Last Post: 12th July 2007, 08:04
  4. Printing a PRN file on an NDPS printer
    By janca in forum General Discussion
    Replies: 4
    Last Post: 20th March 2006, 22:11

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.