Footer and page number for multiple printing using QTextCursor.
-
@IknowQT said in Footer and page number for multiple printing using QTextCursor.:
this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
You probably need to undo that line after printing (set the previously set paint device). The problem is: "print" is local variable and is deleted as soon as it gets out of scope. If Qt tries to paint again it uses a deleted object.
-
@IknowQT I see it complicated since you will see that the implementation cannot be changed from the outside: https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/text/qtextdocument.cpp?h=5.12.11#n1934, my recommendation is that you use QWebEnginePage: create your form using html where you can place the header and footer with html, css and js.
-
https://stackoverflow.com/questions/44537788/page-x-of-y-using-qprinter/44540195#44540195
QPrinter print(QPrinter::PrinterResolution); print.setOutputFormat(QPrinter::PdfFormat); print.setPageSize(QPrinter::A4); print.setOutputFileName("Output.pdf"); QPainter painter(&print); this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print); this->ui.textEdit->document()->setPageSize(print.pageRect().size()); QSizeF pageSize = print.pageRect().size(); // page size in pixels //Calculate the rectangle where to lay out the text const double tm = mmToPixels(print, 12); const qreal footerHeight = painter.fontMetrics().height(); const QRectF textRect(tm, tm, pageSize.width() - 2 * tm, pageSize.height() - 2 * tm - footerHeight); this->ui.textEdit->document()->setPageSize(textRect.size()); const int pageCount = this->ui.textEdit->document()->pageCount(); bool firstPage = true; for (int pageIndex = 0; pageIndex < pageCount; ++pageIndex) { if (!firstPage) print.newPage(); PaintPage(pageIndex, pageCount, &painter, this->ui.textEdit->document(), textRect, footerHeight); firstPage = false; }
this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
I found your article while searching. I applied it like this. Can you let me know if an error occurs after generating a pdf file?
I think the code is the problem, so what should I fix? -
@IknowQT said in Footer and page number for multiple printing using QTextCursor.:
so what should I fix?
What problem do you have?!
-
this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
If you annotate this part, it can be printed, but the screen layout will be broken.
If not annotated, the pdf file is normally created and processed, but the program dies over time.
Call stack tracking is not possible either. -
@IknowQT said in Footer and page number for multiple printing using QTextCursor.:
but the program dies over time
Then please use debugger.
"Call stack tracking is not possible either" - why not? Are you using debug build?
-
@IknowQT said in Footer and page number for multiple printing using QTextCursor.:
this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
You probably need to undo that line after printing (set the previously set paint device). The problem is: "print" is local variable and is deleted as soon as it gets out of scope. If Qt tries to paint again it uses a deleted object.