I'm printing a set of tables, each table should get its own page and could be long. The basics are working, but I don't get the footer painted. The problem is the footer will be painted in an extra document(s).
According to the docs I must set the painter to the device. The device is painter, that's correct, but how do I set the painter to the correct Block? Or is it wrong to act this way?
The goal is to use this document twice. 1st attempt is to print, the second a QTextDocument
where I can pic up the QTextTable
's and to compile it with another document elements.
Working example
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtPrintSupport import *
content = [['section 1', [1,2,3,4]],['section2', [5,6,7,8]]]
app = QApplication(sys.argv)
document = QTextDocument ()
printer = QPrinter()
painter = QPainter(printer)
pageRect = printer.pageRect ()
tableFormat = QTextTableFormat ()
cellBlockFormat = QTextBlockFormat ()
cellCharFormat = QTextCharFormat ()
cellCharFormat.setFont (QFont ("Arial", 10))
for rownr, line in enumerate(content):
cursor = QTextCursor (document)
mainFrame = cursor.currentFrame ()
# header
cursor.setPosition (mainFrame.firstPosition ())
cursor.insertHtml ("This is the table for %s"%line[0])
# table
table = cursor.insertTable (3, 4, tableFormat)
for colnr, col in enumerate(line[1]):
print("col:", col)
cellCursor = table.cellAt (rownr + 1, colnr).firstCursorPosition ()
cellCursor.setBlockFormat (cellBlockFormat)
cellCursor.insertText (str (col))
#footer
painter.begin(printer)
painter.drawText (0, pageRect.bottom(), "I may be the footer")
painter.end()
# section finished
cursor.setPosition (mainFrame.lastPosition ())
tableFormat.setPageBreakPolicy (QTextFormat.PageBreak_AlwaysAfter)
cursor.insertBlock (cellBlockFormat, cellCharFormat)
document.print_(printer)
Premise: this is more a hack than a solution, as it's a dirty workaround.
The idea is to subclass QPrinter, override the
newPage
method and draw the footer accordingly. This requires that theprinter
instance is manually updated with footers.Unfortunately, there's another important catch: I've not been able to print the footer as long as there's only one page.
In the next days I'll try to look into it again, and find if there's a solution for it.