Print multiple QTextDocuments with QPrinter

1.8k Views Asked by At

I need to generate a document to print for a number of objects which the user creates dynamically, and I want to print these documents. I wrote following code (generateDocument() takes a reference to the document to add html code):

QPrinter printer;
QPrintDialog popup(&printer);
if (popup.exec() == QDialog::Accepted)
{
    for (int i = 0; i < _quiz->getSerieCount(); i++)
    {
        QTextDocument doc;
        generateDocument(doc, _quiz->getSerie(i));
        doc.print(&printer);
    }
}

When printing to pdf the behaviour is different in linux and windows: On linux this just prints the last generated document, and on windows it prompts to select a new pdf for every generateDocument() call.

Am i supposed to do this differently?

1

There are 1 best solutions below

1
On

You can add a page break for each serie and then print the document.
Try with the following e.g.

QTextDocument doc;
QTextCursor cursor(&doc);

for (int i = 0; i < _quiz->getSerieCount(); i++)
{
    if(i!=0) \\ dont add page break for the first document
    {
        QTextBlockFormat blockFormat;
        blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysAfter);
        cursor.insertBlock(blockFormat);
    }

    // < append _quiz->getSerie(i) contents in the document >    
}

doc.print(&printer);

Haven't tested the code, but should work on Windows without any problems I suppose, because I was using it similarly without any issues. Can't comment anything for its behavior on Linux machines. You can modify it better to suit your need.
Hope this Helps.