Results 1 to 4 of 4

Thread: Multi page report with QTextDocument

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

    Default Multi page report with QTextDocument

    I am using QTextDocumentto generate html report and so far it works fine, now my report has multi sections witch is require each section on its own page, wrapping sections in table and then insert <div style=\"page-break-after:auto !important;\"></div> didn't work, as QTextDocumenthas limited support for html and css. also I did try to use QPaint, but this draws only the first page of my report.
    Qt Code:
    1. void SemesterResultsReport::printDivisionStudentsNotes() {
    2. QMap<int, int> divisionsList = SemesterResultsReport::getSelectedDivisions();
    3.  
    4. QPrinter *printer = new QPrinter(QPrinter::ScreenResolution);
    5. printer->setFullPage(true);
    6. printer->setResolution(90);
    7. printer->setPaperSize(QPrinter::A4);
    8. printer->setOrientation(QPrinter::Landscape);
    9. printer->setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
    10. /*printer->setOutputFormat(QPrinter::PdfFormat);
    11.   printer->setOutputFileName("sdf.pdf");*/
    12.  
    13.  
    14. QPrintPreviewDialog *dlg = new QPrintPreviewDialog(printer, this);
    15. connect(dlg, SIGNAL(paintRequested(QPrinter *)), this, SLOT(printOrder(QPrinter *)));
    16. dlg->exec();
    17. }
    18.  
    19.  
    20. void SemesterResultsReport::printOrder(QPrinter *printer) {
    21. QString strStream;
    22. QTextStream out(&strStream);
    23.  
    24. QSqlQuery studentInfo, studentNotes;
    25. QSqlRecord studentInfoRec, studentNotesRec;
    26.  
    27. QMap<int, int> selectedDivisions = SemesterResultsReport::getSelectedDivisions();
    28. QMap<int, int>::const_iterator divisionId;
    29.  
    30. QTextDocument *document = new QTextDocument();
    31. QTextCursor cursor(document);
    32. QTextOption options;
    33. options.setTextDirection(Qt::RightToLeft);
    34. QSizeF paperSize;
    35. paperSize.setWidth(printer->width());
    36. paperSize.setHeight(printer->height());
    37. document->setDefaultTextOption(options);
    38. document->setPageSize(paperSize);
    39.  
    40. int mat_div = 0;
    41. int level = 0;
    42. int semester = ui.semestersList->currentText().toInt();
    43. int school_year = 2015;
    44. int numMaterials = 0;
    45.  
    46. // Report
    47. out << "<!DOCTYPE html>"
    48. << "<html>\n"
    49. << "<head>"
    50. << "<title>ff</title>"
    51. << "<meta http-equiv=\"Content-Type\" content =\"text/html;charset=utf-8\" >"
    52. << "<style type=\"text/css\"> "
    53. << " html, body { margin: 5px; direction: rtl; width: 100% !important; align: right !important; float: right !important; }"
    54. << " *, p { font-family: \"Times New Roman\"; font-size: 16px; }"
    55. << " img { display: block; margin: 0 auto; }"
    56. << " p.title { font-weight: bold; font-size: 22px; align: center !important; }"
    57. << " table { border: 1; border-collapse: collapse; page-break-after:auto !important; width: 100% !important; align: right !important; float: right !important; }"
    58. << " th, td { border: 1px solid #000; padding: 0; align: center; page-break-inside:avoid; page-break-after:auto; }"
    59. << " tr { page-break-inside:avoid; page-break-after:auto !important; }"
    60.  
    61. << " thead { display:table-header-group; }"
    62. << " tfoot { display:table-footer-group; }"
    63. << " .pagebreak { page-break-after:auto !important; } "
    64. << "</style>"
    65. << "</head>"
    66. << "<body>";
    67. for (divisionId = selectedDivisions.constBegin(); divisionId != selectedDivisions.constEnd(); ++divisionId) {
    68. mat_div = divisionId.key();
    69. level = divisionId.value();
    70. numMaterials = 0;
    71.  
    72. // Report header, get division materials to set as header
    73. QStringList divisionMaterialsNames = SemesterResultsReport::getDivisionMaterialsNames(mat_div, level, school_year);
    74. out << "<table float=\"right\" border=1 cellspacing=0 cellpadding=2>";
    75. numMaterials = divisionMaterialsNames.size();
    76. out << "<thead><tr bgcolor=#f0f0f0>";
    77. for (int i = numMaterials - 1; i > -1; --i) {
    78. out << "<th>" + divisionMaterialsNames[i] + "</th>";
    79. }
    80.  
    81. out << "<th>" + QString("الإسم") + "</th>"
    82. << "<th>" + QString("اللقب") + "</th>"
    83. << "<th>" + QString("الرقم التسلسلي") + "</th>"
    84. << "</tr></thead>";
    85.  
    86. // Echo student info plus materials notes
    87. QString fullName = ", fname, lname";
    88. QString infoQuery = "SELECT Student.mat_stud as matStud" + fullName + " FROM Student, Class, Division WHERE Class.mat_div = Division.mat_div AND Class.mat_class = Student.mat_class AND school_year = " + QString::number(school_year) + " AND Class.level = " + QString::number(level) + " AND Division.mat_div = " + QString::number(mat_div);
    89. studentInfo.exec(infoQuery);
    90. studentInfoRec = studentInfo.record();
    91. fullName.clear();
    92. infoQuery = "SELECT Student.mat_stud as matStud FROM Student, Class, Division WHERE Class.mat_div = Division.mat_div AND Class.mat_class = Student.mat_class AND school_year = " + QString::number(school_year) + " AND Class.level = " + QString::number(level) + " AND Division.mat_div = " + QString::number(mat_div);
    93. QString notesQuery = "SELECT materials_notes FROM Notes WHERE mat_stud IN (" + infoQuery + ") AND school_year = " + QString::number(school_year) + " AND level = " + QString::number(level) + " AND mat_div = " + QString::number(mat_div) + " AND semester = " + QString::number(semester);
    94. studentNotes.exec(notesQuery);
    95. studentNotesRec = studentNotes.record();
    96. QStringList studentNotesList;
    97. while (studentInfo.next()) {
    98. out << "<tr>";
    99. studentNotes.next();
    100. studentNotesList = studentNotes.value(studentNotesRec.indexOf("materials_notes")).toString().split(",");
    101. for (int i = 0; i < numMaterials; ++i) {
    102. out << "<th>" + studentNotesList[i] + "</th>";
    103. }
    104.  
    105. out << " <th>" + studentInfo.value(studentInfoRec.indexOf("lname")).toString() + "</th>"
    106. << " <th>" + studentInfo.value(studentInfoRec.indexOf("fname")).toString() + "</th>"
    107. << " <th>" + studentInfo.value(studentInfoRec.indexOf("matStud")).toString() + "</th>";
    108.  
    109. out << "</tr>";
    110. }
    111.  
    112. // Close table tag, and insert new page, but it doesn't work
    113. out << "</table><div style=\"page-break-after:auto !important;\"></div>";
    114. }
    115.  
    116. // Finish report
    117. out << "</body>"
    118. << "</html>";
    119.  
    120.  
    121. /*
    122.   * Prepare QTextDocument
    123.   */
    124. document->setHtml(strStream);
    125. document->print(printer);
    126. // this makes only the first page printed
    127. /*QPainter painter;
    128.   painter.begin(printer);
    129.   document->drawContents(&painter, printer->pageRect());
    130.   painter.end();*/
    131. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multi page report with QTextDocument

    If the method of filling a QTextDocument by HTML import doesn't provide all the necessary features, it might still be possible using QTextDocument's own API.

    Alternatively you could look at using a report generator library, e.g KDReports https://github.com/KDAB/KDReports

    Cheers,
    _

  3. #3
    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: Multi page report with QTextDocument

    That attribute is in the supported HTML subset but it is may only be supported on <p> and <table> elements. The "auto" value leaves the decision up to the layout engine... You might want "always".

    See the following post for another way to paginate from QTextDocument:
    http://www.qtcentre.org/threads/5779...not-breakering
    You can use this method with a little thought to do things like headers and footers.

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

    Default Re: Multi page report with QTextDocument

    I am looking for a simple solution as possible. QTextDocument has it's own API but to me is little bit complex to generate clean report.

    @ChrisW67
    I tried that too, but it s still not breaking pages. For the thread you refer to, I had read it, witch I am already trying to avoid because my knowledge in QTextDocument is and reports construction is limited.
    Last edited by H Aßdøµ; 16th February 2015 at 00:51.

Similar Threads

  1. Report, QTextDocument (memory leak)
    By layer in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2012, 08:55
  2. Replies: 2
    Last Post: 14th October 2011, 10:36
  3. problem printing a multi page report
    By schnitzel in forum Newbie
    Replies: 12
    Last Post: 23rd January 2011, 02:50
  4. multi page Application
    By ilpaso in forum Qt Programming
    Replies: 2
    Last Post: 3rd September 2010, 10:36
  5. Multi-Page PDF Output
    By igor in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 05:10

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.