带有QTextDocument的多页报告

带有QTextDocument的多页报告,qt,qt5,qtextdocument,qprinter,Qt,Qt5,Qtextdocument,Qprinter,我正在用QTextDocument生成html报告,我的问题是它应该在自己的页面上打印每个表,但它没有。我尝试将QPainter与QTextDocument::drawContents()一起使用,但这使得它只显示第一页。 另一个尝试是插入一个div,并将其样式设置为page break after:auto,即使它受到QTextDocumentweb引擎的支持,但这也不起作用 void SemesterResultsReport::printDivisionStudentsNotes() {

我正在用
QTextDocument
生成html报告,我的问题是它应该在自己的页面上打印每个表,但它没有。我尝试将
QPainter
QTextDocument::drawContents()
一起使用,但这使得它只显示第一页。 另一个尝试是插入一个
div
,并将其样式设置为p
age break after:auto
,即使它受到
QTextDocument
web引擎的支持,但这也不起作用

void SemesterResultsReport::printDivisionStudentsNotes() {
    QMap<int, int> divisionsList = SemesterResultsReport::getSelectedDivisions();

    QPrinter *printer = new QPrinter(QPrinter::ScreenResolution);
    printer->setFullPage(true);
    printer->setResolution(90);
    printer->setPaperSize(QPrinter::A4);
    printer->setOrientation(QPrinter::Landscape);
    printer->setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
    /*printer->setOutputFormat(QPrinter::PdfFormat);
    printer->setOutputFileName("sdf.pdf");*/


    QPrintPreviewDialog *dlg = new QPrintPreviewDialog(printer, this);
    connect(dlg, SIGNAL(paintRequested(QPrinter *)), this, SLOT(printOrder(QPrinter *)));
    dlg->exec();
}


void SemesterResultsReport::printOrder(QPrinter *printer) {
    QString strStream;
    QTextStream out(&strStream);

    QSqlQuery studentInfo, studentNotes;
    QSqlRecord studentInfoRec, studentNotesRec;

    QMap<int, int> selectedDivisions = SemesterResultsReport::getSelectedDivisions();
    QMap<int, int>::const_iterator divisionId;

    QTextDocument *document = new QTextDocument();
    QTextCursor cursor(document);
    QTextOption options;
    options.setTextDirection(Qt::RightToLeft);
    QSizeF paperSize;
    paperSize.setWidth(printer->width());
    paperSize.setHeight(printer->height());
    document->setDefaultTextOption(options);
    document->setPageSize(paperSize);

    int mat_div = 0;
    int level = 0;
    int semester = ui.semestersList->currentText().toInt();
    int school_year = 2015;
    int numMaterials = 0;

    // Report
    out << "<!DOCTYPE html>"
        << "<html>\n"
        << "<head>"
        << "<title>ff</title>"
        << "<meta http-equiv=\"Content-Type\" content =\"text/html;charset=utf-8\" >"
        << "<style type=\"text/css\"> "
        << "  html, body { margin: 5px; direction: rtl; width: 100% !important; align: right !important; float: right !important; }"
        << "  *, p { font-family: \"Times New Roman\"; font-size: 16px; }"
        << "  img { display: block; margin: 0 auto; }"
        << "  p.title { font-weight: bold; font-size: 22px; align: center !important; }"
        << "  table { border: 1; border-collapse: collapse; page-break-after:auto !important; width: 100% !important; align: right !important; float: right !important; }"
        << "  th, td    { border: 1px solid #000; padding: 0; align: center; page-break-inside:avoid; page-break-after:auto; }"
        << "  tr    { page-break-inside:avoid; page-break-after:auto !important; }"

        << "  thead { display:table-header-group; }"
        << "  tfoot { display:table-footer-group; }"
        << "  .pagebreak { page-break-after:auto !important; } "
        << "</style>"
        << "</head>"
        << "<body>";
    for (divisionId = selectedDivisions.constBegin(); divisionId != selectedDivisions.constEnd(); ++divisionId) {
        mat_div = divisionId.key();
        level = divisionId.value();
        numMaterials = 0;

        // Report header, get division materials to set as header
        QStringList divisionMaterialsNames = SemesterResultsReport::getDivisionMaterialsNames(mat_div, level, school_year);
        out << "<table float=\"right\" border=1 cellspacing=0 cellpadding=2>";
        numMaterials = divisionMaterialsNames.size();
        out << "<thead><tr bgcolor=#f0f0f0>";
        for (int i = numMaterials - 1; i > -1; --i) {
            out << "<th>" + divisionMaterialsNames[i] + "</th>";
        }

        out << "<th>" + QString("الإسم") + "</th>"
            << "<th>" + QString("اللقب") + "</th>"
            << "<th>" + QString("الرقم التسلسلي") + "</th>"
            << "</tr></thead>";

        // Echo student info plus materials notes
        QString fullName = ", fname, lname";
        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);
        studentInfo.exec(infoQuery);
        studentInfoRec = studentInfo.record();
        fullName.clear();
        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);
        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);
        studentNotes.exec(notesQuery);
        studentNotesRec = studentNotes.record();
        QStringList studentNotesList;
        while (studentInfo.next()) {
            out << "<tr>";
            studentNotes.next();
            studentNotesList = studentNotes.value(studentNotesRec.indexOf("materials_notes")).toString().split(",");
            for (int i = 0; i < numMaterials; ++i) {
                out << "<th>" + studentNotesList[i] + "</th>";
            }

            out << "    <th>" + studentInfo.value(studentInfoRec.indexOf("lname")).toString() + "</th>"
                << "    <th>" + studentInfo.value(studentInfoRec.indexOf("fname")).toString() + "</th>"
                << "    <th>" + studentInfo.value(studentInfoRec.indexOf("matStud")).toString() + "</th>";

            out << "</tr>";
        }

        // Close table tag, and insert new page, but it doesn't work
        out << "</table><div style=\"page-break-after:auto !important;\"></div>";
    }

    // Finish report
    out << "</body>"
        << "</html>";


    /*
    * Prepare QTextDocument
    */
    document->setHtml(strStream);
    document->print(printer);
    // this makes only the first page printed
    /*QPainter painter;
    painter.begin(printer);
    document->drawContents(&painter, printer->pageRect());
    painter.end();*/
}
void semestResultsReport::PrintDivision StudentsNotes(){
QMap divisions list=semestResultsReport::getSelectedDivisions();
QPrinter*打印机=新的QPrinter(QPrinter::屏幕分辨率);
打印机->设置整页(真);
打印机->设置分辨率(90);
打印机->设置纸张尺寸(QPrinter::A4);
打印机->设置方向(QPrinter::横向);
打印机->设置页边距(5,5,5,5,QPrinter::毫米);
/*打印机->设置输出格式(QPrinter::PdfFormat);
打印机->设置输出文件名(“sdf.pdf”)*/
QPrintPreviewDialog*dlg=新的QPrintPreviewDialog(打印机,本机);
连接(dlg,信号(已请求油漆(QPrinter*)),此,插槽(printOrder(QPrinter*));
dlg->exec();
}
void SemesterResultsReport::printOrder(QPrinter*打印机){
QString stream;
QTextStream out(&strStream);
qsqlquerystudentinfo,studentNotes;
QSqlRecord StudentInfo记录,studentNotesRec;
QMap selectedDivisions=semestresultsreport::getSelectedDivisions();
QMap::const_迭代器divisionId;
QTextDocument*document=新的QTextDocument();
QTextCursor(文档);
期权;
options.setTextDirection(Qt::rightoleft);
QSizeF纸张尺寸;
paperSize.setWidth(打印机->宽度());
paperSize.setHeight(打印机->高度());
文档->设置默认文本选项(选项);
文档->设置页面大小(纸张大小);
int mat_div=0;
智力水平=0;
int EXTERM=ui.semestList->currentText().toInt();
国际学年=2015年;
int numMaterials=0;
//报告

out您不应该在:always;
之后使用
分页符,而不是为每个表使用
自动