Results 1 to 6 of 6

Thread: Issues with QTextDocument and QPrintPreviewDialog

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.