alpinRunnrer
alpinRunnrer

Reputation: 21

Multi page report with QTextDocument using QTextEdit

I created a QTextDocument with several blocks of QTextEdit handling HTML inside. I want to do page breaks only after a block ends. but I dont want to do a page break after each block. Page can having several blocks. How can I force page breaks (in HTLM or from QTextDocument)?

'''QPrinterInfo l_PrinterInfo = QPrinterInfo::printerInfo(p_PrinterName);
QPrinter l_DefaultPrinter(l_PrinterInfo);
l_DefaultPrinter.setOrientation(QPrinter::Portrait);
l_DefaultPrinter.setResolution(QPrinter::HighResolution);
l_DefaultPrinter.setPaperSize(QPrinter::A4);

QPageSize::PageSizeId pageSizeId = l_DefaultPrinter.pageLayout().pageSize().id();
QRectF rect = l_DefaultPrinter.pageRect(QPrinter::Millimeter);
QSizeF rectPagePrinter(rect.width(), rect.height());

QTextDocument doc;
//set global styte to document
doc.setDefaultStyleSheet(style);
//set document page size
doc.setPageSize(rectPagePrinter);

QTextEdit textEdit;
textEdit.setDocument(&doc);
//first page
//first table block 
textEdit.textCursor().insertHtml(htmlStringTable);
//second imgage block
textEdit.textCursor().insertHtml(htmlImg);
//third pagefooter block
textEdit.textCursor().insertHtml(pageFooter);
//do a break page DO NOT WORK!
QTextBlockFormat blockFormat;
blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysAfter);
textEdit.textCursor().insertBlock(blockFormat);

// second page

textEdit.textCursor().insertHtml(htmlStringTable2);
textEdit.textCursor().insertHtml(htmlImgScreenShot);
textEdit.textCursor().insertHtml(pageFooter);

//send to the printer

doc.print(&l_DefaultPrinter);

'''

Upvotes: 2

Views: 425

Answers (1)

Misinahaiya
Misinahaiya

Reputation: 713

You can simply do

textEdit.textCursor().block().setPageBreakPolicy(QTextFormat::PageBreak_AlwaysAfter);

or

__editor.textCursor().block().setPageBreakPolicy(QTextFormat.PageBreak_AlwaysAfter)

if you’re using the Python language.

P.S.

but I dont want to do a page break after each block

Just do the above operation. Qt won’t let the blocks down. Unless you write above code to every QTextBlocks in a QTextDocument.

Upvotes: 1

Related Questions