Results 1 to 3 of 3

Thread: Printing using QTextLayout

  1. #1
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question Printing using QTextLayout

    Hello,

    I have programmed a simple code editor, which uses the QSyntaxHighlighter.
    And now i have a problem with my print method.

    I had to implement a new one since I need to print a header and footer on each page and I also need to print the corresponding line numbers besides the text.

    After much experimentation I have almost reached my "goal".

    First i get the pageRect from the given QPrinter and calculate the size for the header, footer and line numbers from which i then get the actual draw area for the document.
    Then I break down the QTextDocument into its QTextBlocks in order to iterate over them within the printing process. (using a while(block.isValid())... loop)

    Within the loop i break down the blocks into QTextLayouts (for the case that a block has one or more linebraks) This is also important in order to find the proper horizontal position for the line number and for the case that a block needs to be broken up between page breaks.

    My current problem shows its (ugly) face after a page break where the textLayout.draw(...) method decides to ignore the horizontal position the first line and draw a few pixels off target, invading the space for the next line.
    This only occurs in the first line of pages 2 and up. Page 1 is printed properly.

    Can anyone give me a hint why the draw Method would be ignoring the QPoint given to it?
    I use the same horizontal offset when drawing the line number which is placed properly in the page.

    Thank you for reading and maybe helping

    Eric

  2. #2
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: Printing using QTextLayout

    So for better understandig i am uploading two images of a printed out pdf (I am using a PDF printer for debugging).

    And some code:

    Qt Code:
    1. void TextEditorWidget::printDocument(QPrinter *printer)
    2. {
    3. QTextDocument *document = editor->document();
    4. //Header & Footer .... just building the strings...
    5.  
    6. QPainter printPainter(printer);
    7. printPainter.setFont(f); //<- f: is the font used as standard in the QTextEdit
    8. QRect pageRect = printer->pageRect();
    9. QRect headerRect = printPainter.boundingRect(printPainter.window()/*pageRect*/,Qt::AlignLeft |Qt::AlignTop, printHeader);
    10. QRect footerRect = printPainter.boundingRect(printPainter.window()/*pageRect*/,Qt::AlignBottom |Qt::AlignHCenter, footer);
    11. QRect numbersRect = printPainter.boundingRect(printPainter.window()/*pageRect*/,Qt::AlignLeft,
    12. QString::number(document->blockCount()));
    13. pageRect.setTop(headerRect.height() +17);// adding a spacing
    14. pageRect.setLeft(numbersRect.width() +8);
    15. pageRect.setBottom(footerRect.top() -10);
    16. pageRect.setRight(/*pageRect.left() + printer->pageRect().width()*/ printPainter.window().right());
    17. document->setPageSize(pageRect.size());
    18.  
    19. QAbstractTextDocumentLayout *lay = document->documentLayout();
    20. lay->setPaintDevice(printer);
    21. int pages = lay->pageCount();
    22.  
    23. QTextBlock block = document->begin();
    24. int pageNumber = 0;
    25. int yOffset = 0;
    26. bool newPage = true;
    27. printPainter.setClipping(true); //thought this might have something to do with the porblem, but i couldnt see any difference whether set true or false
    28.  
    29. while(block.isValid()) //<- Iteration Loop
    30. {
    31. if(newPage)
    32. {
    33. ++ pageNumber;
    34. printPainter.drawText(QRect(QPoint(0,0),headerRect.size()),Qt::AlignLeft, printHeader);
    35. printPainter.drawText(printPainter.window(),Qt::AlignBottom|Qt::AlignHCenter,
    36. footer.arg(QString::number(pageNumber),QString::number(pages)));
    37. yOffset = pageRect.top();
    38. newPage = false;
    39. }
    40.  
    41. QTextLayout *lT = block.layout();
    42. for (int i=0; i< lT->lineCount(); i++) //Line Iteration usualy 1 line per Block
    43. {
    44. if(i==0) printPainter.drawText(QRect(QPoint(0,yOffset),numbersRect.size()),Qt::AlignRight,
    45. QString::number(block.blockNumber()+1));
    46. lT->lineAt(i).draw(&printPainter,QPointF(pageRect.left(),yOffset));
    47. }
    48. yOffset += lT->boundingRect().height(); // Increment Horizontal offset by size of last Block
    49. if (yOffset >= printPainter.window().bottom()- (footerRect.height()+17+10))
    50.  
    51. {
    52. newPage = true;
    53. printer->newPage();
    54. }
    55. block = block.next();
    56. }
    57. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  3. #3
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Printing using QTextLayout

    Update:

    After some more debugging, I noticed that the QTextLine recieved a worng position for the lines where the error ocurred (usually QPiontF(0,0)) but here it recieved a QPointF(0,10)
    I haven't figuered out why this happens, but at least I was able to reset it using the QTextLine::setPosition(...) method.

    If anyone is working with this type of text drawing i must add that you will need to manage the position of the linebreaks as these will need a different y-coordinate (usualy linenumber * lineHeight)


    Greetings

Similar Threads

  1. Printing txt file vs Printing pdf file
    By rmagro in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2012, 16:45
  2. Replies: 4
    Last Post: 19th May 2009, 09:06
  3. Qt4 poor printing performance
    By seneca in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2009, 15:23
  4. Printing problem in windows
    By joseph in forum Qt Programming
    Replies: 6
    Last Post: 12th July 2007, 09:04
  5. Replies: 0
    Last Post: 28th June 2006, 21:49

Tags for this Thread

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.