PDA

View Full Version : How to prevent a page break in the middle of a QTextFrame while printing



brunoqc
11th November 2010, 08:58
Hi,

With this sample code, I'm trying to do 7 frames with 8 lines of text each. When I print it in a PDF, the first line of the 7 frame is on the first page and the rest of the same frame is on the second page.

I would like that if the entire frame doesn't fit on the page, to have a page break before it to ensure the frame are never split.

I tough the "frameFormat.setPageBreakPolicy(QTextFormat::PageBr eak_Auto);" would be enough.

Is there a way to do it? Or is it possible to know in advance if a frame will be split and do a "frameFormat.setPageBreakPolicy(QTextFormat::PageBr eak_AlwaysBefore);" only for that frame?

thanks!


QTextDocument *doc = ui->textEdit->document();

QTextCursor cursor(doc);

for (int j=1; j<=7; j++) {
QTextFrameFormat frameFormat;
if (j % 2) {
frameFormat.setBackground(Qt::red);
}
frameFormat.setPageBreakPolicy(QTextFormat::PageBr eak_Auto);

cursor.movePosition(QTextCursor::NextBlock);
cursor.insertFrame(frameFormat);

for (int i=1; i<=8; i++) {
cursor.insertText(QString("frame %1 - line %2\n").arg(QString::number(j),
QString::number(i)));
}
}

QPrinter *printer = new QPrinter();
QPrintDialog printDialog(printer, this);
if (printDialog.exec() == QDialog::Accepted) {
doc->print(printer);
}