Results 1 to 4 of 4

Thread: QPrinter pages not breakering

  1. #1
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default QPrinter pages not breakering

    Hello,

    I am generating a QTextTableand inserting it into a QTextDocument and printing to PDF.
    The table is not automatically split and the next page is not generated.
    Here is the code:
    Qt Code:
    1. ...
    2. void MainWindow::generatePdf()
    3. {
    4. QPrinter printer;
    5. QPainter painter;
    6.  
    7. QString pdfFile = QFileDialog::getSaveFileName(this);
    8. printer.setOutputFormat(QPrinter::PdfFormat);
    9. printer.setOutputFileName(pdfFile);
    10. printer.setPageSize(QPrinter::A4);
    11. printer.setPageMargins (15,15,15,15,QPrinter::Millimeter);
    12.  
    13. painter.begin(&printer);
    14.  
    15. QTextEdit *editor = new QTextEdit();
    16. QTextDocument *document = new QTextDocument(editor);
    17. QTextCursor cursor(document);
    18.  
    19. QTextTable *table = cursor.insertTable(100, 1);
    20. cursor.insertText("test");
    21. for(int i = 0; i < table->rows() - 1; i++) {
    22. cursor = table->cellAt(i + 1, 0).firstCursorPosition();
    23. cursor.insertText("Row : " + QString::number(i + 1));
    24. }
    25.  
    26. document->drawContents(&painter);
    27. painter.end();
    28. }
    29. ...
    To copy to clipboard, switch view to plain text mode 

    How can this be fixed?
    I have attachment the generate PDF file.

    Thank you
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPrinter pages not breakering

    You have chosen to paint the QTextDocument directly. You get to do the pagination yourself.

    See QTextDocument::print()

  3. #3
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QPrinter pages not breakering

    Quote Originally Posted by ChrisW67 View Post
    You have chosen to paint the QTextDocument directly. You get to do the pagination yourself.

    See QTextDocument::print()
    Hello ChrisW67,
    I chose to paint the QTextDocument directly because it is a requirement for QPrinter::newPage();
    It would be nice to have a signal for when the QTextDocument would paginate itself....
    Thanks

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPrinter pages not breakering

    Quote Originally Posted by scarecr0w132 View Post
    I chose to paint the QTextDocument directly because it is a requirement for QPrinter::newPage();
    If you want QTextDocument to paginate and render itself to a printer then use QTextDocument::print(). If you want to control page breaks yourself, or do things like headers/footers or multiple pages per sheet, then use drawContents() and call newPage() yourself. It's not too difficult, something like:
    Qt Code:
    1. printer->setPageMargins(15, 15, 15, 15, QPrinter::Millimeter);
    2. const QRect pageRect = printer->pageRect();
    3.  
    4. // Give the document the correct left-to-right width for word wrapping etc
    5. doc.setPageSize(pageRect.size());
    6.  
    7. // The total extent of the content (there are no page margin in this)
    8. QRect contentRect = QRect(QPoint(0, 0), doc.size().toSize());
    9.  
    10. // This is the part of the content we will drop on a page. It's a sliding window on the content.
    11. QRect currentRect(0, 0, pageRect.width(), pageRect.height());
    12.  
    13. QPainter painter(printer);
    14. while (currentRect.intersects(contentRect)) {
    15. painter.save();
    16. painter.translate(0, -currentRect.y());
    17. doc.drawContents(&painter, currentRect); // draws part of the document
    18. painter.restore();
    19.  
    20. // Translate the current rectangle to the area to be printed for the next page
    21. currentRect.translate(0, currentRect.height());
    22.  
    23. //Inserting a new page if there is still area left to be printed
    24. if (currentRect.intersects(contentRect))
    25. printer->newPage();
    26. }
    To copy to clipboard, switch view to plain text mode 

    It would be nice to have a signal for when the QTextDocument would paginate itself....
    If you are using QTextDocument::drawContents() then the QTextDocument does not dictate where page breaks go.

  5. The following user says thank you to ChrisW67 for this useful post:

    Cupidvogel (11th February 2016)

Similar Threads

  1. print several pages
    By beirrascan in forum Newbie
    Replies: 1
    Last Post: 8th November 2010, 22:26
  2. QTableView with pages
    By sysmaniac in forum Newbie
    Replies: 1
    Last Post: 4th November 2010, 18:11
  3. QPrinter::PrinterMode and QPrinter::setResolution??
    By SkripT in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2006, 11:59
  4. Replies: 3
    Last Post: 21st April 2006, 10:58
  5. switching between pages
    By Kapil in forum Newbie
    Replies: 9
    Last Post: 6th March 2006, 10:57

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.