Results 1 to 6 of 6

Thread: Issues with QTextDocument and QPrintPreviewDialog

  1. #1
    Join Date
    Jan 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Issues with QTextDocument and QPrintPreviewDialog

    Hello,
    I have strange problems seems I don't know even from where I should start. Based on this answer , and after modification, looks like QPrintPreviewDialogprints only one page from the report, even it should print at least 3 pages, as my model have over 250 row.
    The second problem, after I print the page, it printed in very small size, even when it appears on QPrintPreviewDialogas it feat the page width.

    Qt Code:
    1. void StudentNotes::printStudentsList()
    2. {
    3. QPrinter *printer = new QPrinter(QPrinter::PrinterResolution);
    4. printer->setFullPage(true);
    5. printer->setResolution( 90 );
    6. printer->setPaperSize(QPrinter::A4);
    7. printer->setOrientation(QPrinter::Landscape);
    8. printer->setPageMargins (15,15,15,15, QPrinter::Millimeter);
    9.  
    10. QPrintPreviewDialog *dlg = new QPrintPreviewDialog(printer, this);
    11. connect(dlg, SIGNAL(paintRequested(QPrinter *)), this, SLOT(setupStudentsListPrint(QPrinter *)));
    12. dlg->exec();
    13. }
    14.  
    15. void StudentNotes::setupStudentsListPrint(QPrinter *printer)
    16. {
    17. QPainter painter;
    18. painter.begin(printer);
    19. QString strStream;
    20. QTextStream out(&strStream);
    21.  
    22. const int rowCount = ui->listStudentsTable->model()->rowCount();
    23. const int columnCount = ui->listStudentsTable->model()->columnCount();
    24. out << "<html dir=\"rtl\">\n"
    25. "<head>\n"
    26. "<meta Content=\"text/html; charset=utf-8\">\n"
    27. << QString("<title>%1</title>\n").arg("Print test")
    28. << "<style> "
    29. << " html, body { width: 100%; padding: 0; margin: 0; font-size: 16px; }"
    30. << " table { page-break-inside:avoid }"
    31. << " tr { page-break-inside:avoid; page-break-after:auto }"
    32. << " thead { display:table-header-group }"
    33. << " tfoot { display:table-footer-group }"
    34. << "</style>"
    35. << "</head>\n"
    36. "<body bgcolor=#ffffff link=#5000A0>\n"
    37. "<table border=1 cellspacing=0 cellpadding=2>\n";
    38.  
    39. // headers
    40. out << "<thead><tr bgcolor=#f0f0f0>";
    41. for (int column = 0; column < columnCount; column++)
    42. if (!ui->listStudentsTable->isColumnHidden(column))
    43. out << QString("<th>%1</th>").arg(ui->listStudentsTable->model()->headerData(column, Qt::Horizontal).toString());
    44. out << "</tr></thead>\n";
    45.  
    46. // data table
    47. for (int row = 0; row < rowCount; row++) {
    48. out << "<tr>";
    49. for (int column = 0; column < columnCount; column++) {
    50. if (!ui->listStudentsTable->isColumnHidden(column)) {
    51. QString data = ui->listStudentsTable->model()->data(ui->listStudentsTable->model()->index(row, column)).toString().simplified();
    52. out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString("&nbsp;"));
    53.  
    54. }
    55. }
    56. out << "</tr>\n";
    57. }
    58. out << "</table>\n"
    59. "</body>\n"
    60. "</html>\n";
    61.  
    62. // Just for debugging purposes
    63. QFile file("htmlFileName.html");
    64. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
    65. //MSG(QString("Can`t create file %1").arg(htmlFileName));
    66. }
    67.  
    68. file.write(strStream.toLatin1());
    69. file.close();
    70. QSizeF paperSize;
    71. paperSize.setWidth(printer->width());
    72. paperSize.setHeight(printer->height());
    73. QTextDocument *document = new QTextDocument();
    74. QTextOption options;
    75. options.setTextDirection(Qt::RightToLeft);
    76. document->setDefaultTextOption(options);
    77. document->setPageSize(paperSize);
    78. document->setHtml(strStream);
    79. document->drawContents(&painter);
    80. painter.end();
    81. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by H Aßdøµ; 17th January 2015 at 03:02.

  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: Issues with QTextDocument and QPrintPreviewDialog

    You only ever paint on the first page, i.e. you never call newPage() on the printer. You get whatever will fit on the current page and the rest will be chopped.

    If you have a multi-page document in a QTextDocument then you should use QTextDocument::print() to get free pagination.

  3. #3
    Join Date
    Jan 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Issues with QTextDocument and QPrintPreviewDialog

    Replacing QPainterwith this line shows all pages to send to the printer:
    Qt Code:
    1. document->print(printer);
    To copy to clipboard, switch view to plain text mode 
    Now, if I print directly from QPrintPreviewDialogto the printer, the printed text is too small, and by too small I mean as micro text. But when I printed as PDF file then print the pdf file the text printed showed as normal size, what should I do now?

  4. #4
    Join Date
    Jan 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Issues with QTextDocument and QPrintPreviewDialog

    The too small text caused by calling QTextDocument::setPageSize before the document set any html, so swapping this two lines fixes the issue of tiny font text.
    Qt Code:
    1. document->setHtml(strStream);
    2. document->setPageSize(paperSize);
    To copy to clipboard, switch view to plain text mode 
    My last problem with printing(I hope so) is my report is right to left(Arabic language), but QPrintPreviewDialog shows it as left to right, even I set dir attribute to rtl in html tag.

  5. #5
    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: Issues with QTextDocument and QPrintPreviewDialog

    Take a look at the Supported HTML Subset for QTextDocument to see why setting the dir attribute on the html element is not working. Trying wrapping the content of the body element in a div element.

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

    H Aßdøµ (15th February 2015)

  7. #6
    Join Date
    Jan 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Issues with QTextDocument and QPrintPreviewDialog

    That is bad. Even I after switched to QWebView, it has issues when it comes to show images.

Similar Threads

  1. QPrintPreviewDialog help
    By Yaoming in forum Qt Programming
    Replies: 4
    Last Post: 28th February 2014, 13:55
  2. QPrintPreviewDialog linux
    By davidovv in forum Qt Programming
    Replies: 0
    Last Post: 12th February 2013, 01:10
  3. Pratical example of QPrintPreviewDialog
    By vcp in forum Qt Programming
    Replies: 5
    Last Post: 24th July 2012, 17:24
  4. QPrintPreviewDialog.
    By cydside in forum Qt Programming
    Replies: 2
    Last Post: 19th June 2009, 18:31
  5. QPrintPreviewDialog icons
    By quipu5 in forum Qt Programming
    Replies: 0
    Last Post: 19th January 2009, 14:36

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.