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