PDA

View Full Version : QPrinter pages not breakering



scarecr0w132
26th January 2014, 10:03
Hello,

I am generating a QTextTable and inserting it into a QTextDocument and printing to PDF.
The table is not automatically split and the next page is not generated.
Here is the code:


...
void MainWindow::generatePdf()
{
QPrinter printer;
QPainter painter;

QString pdfFile = QFileDialog::getSaveFileName(this);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(pdfFile);
printer.setPageSize(QPrinter::A4);
printer.setPageMargins (15,15,15,15,QPrinter::Millimeter);

painter.begin(&printer);

QTextEdit *editor = new QTextEdit();
QTextDocument *document = new QTextDocument(editor);
QTextCursor cursor(document);

QTextTable *table = cursor.insertTable(100, 1);
cursor.insertText("test");
for(int i = 0; i < table->rows() - 1; i++) {
cursor = table->cellAt(i + 1, 0).firstCursorPosition();
cursor.insertText("Row : " + QString::number(i + 1));
}

document->drawContents(&painter);
painter.end();
}
...


How can this be fixed?
I have attachment the generate PDF file.

Thank you

ChrisW67
26th January 2014, 21:15
You have chosen to paint the QTextDocument directly. You get to do the pagination yourself.

See QTextDocument::print()

scarecr0w132
27th January 2014, 10:58
You have chosen to paint the QTextDocument directly. You get to do the pagination yourself.

See QTextDocument::print()

Hello ChrisW67,
I chose to paint the QTextDocument directly because it is a requirement for QPrinter::newPage();
It would be nice to have a signal for when the QTextDocument would paginate itself....
Thanks

ChrisW67
28th January 2014, 23:06
I chose to paint the QTextDocument directly because it is a requirement for QPrinter::newPage();
If you want QTextDocument to paginate and render itself to a printer then use QTextDocument::print(). If you want to control page breaks yourself, or do things like headers/footers or multiple pages per sheet, then use drawContents() and call newPage() yourself. It's not too difficult, something like:


printer->setPageMargins(15, 15, 15, 15, QPrinter::Millimeter);
const QRect pageRect = printer->pageRect();

// Give the document the correct left-to-right width for word wrapping etc
doc.setPageSize(pageRect.size());

// The total extent of the content (there are no page margin in this)
QRect contentRect = QRect(QPoint(0, 0), doc.size().toSize());

// This is the part of the content we will drop on a page. It's a sliding window on the content.
QRect currentRect(0, 0, pageRect.width(), pageRect.height());

QPainter painter(printer);
while (currentRect.intersects(contentRect)) {
painter.save();
painter.translate(0, -currentRect.y());
doc.drawContents(&painter, currentRect); // draws part of the document
painter.restore();

// Translate the current rectangle to the area to be printed for the next page
currentRect.translate(0, currentRect.height());

//Inserting a new page if there is still area left to be printed
if (currentRect.intersects(contentRect))
printer->newPage();
}



It would be nice to have a signal for when the QTextDocument would paginate itself....
If you are using QTextDocument::drawContents() then the QTextDocument does not dictate where page breaks go.