<tfoot id='pxjNF'></tfoot><legend id='pxjNF'><style id='pxjNF'><dir id='pxjNF'><q id='pxjNF'></q></dir></style></legend>
    <bdo id='pxjNF'></bdo><ul id='pxjNF'></ul>

  1. <small id='pxjNF'></small><noframes id='pxjNF'>

    <i id='pxjNF'><tr id='pxjNF'><dt id='pxjNF'><q id='pxjNF'><span id='pxjNF'><b id='pxjNF'><form id='pxjNF'><ins id='pxjNF'></ins><ul id='pxjNF'></ul><sub id='pxjNF'></sub></form><legend id='pxjNF'></legend><bdo id='pxjNF'><pre id='pxjNF'><center id='pxjNF'></center></pre></bdo></b><th id='pxjNF'></th></span></q></dt></tr></i><div id='pxjNF'><tfoot id='pxjNF'></tfoot><dl id='pxjNF'><fieldset id='pxjNF'></fieldset></dl></div>

      为 QPrintWidget、QPrintPrevwiwWidget 添加页脚

      时间:2023-08-04

        • <tfoot id='RCxIs'></tfoot>

            <small id='RCxIs'></small><noframes id='RCxIs'>

              <tbody id='RCxIs'></tbody>
          1. <legend id='RCxIs'><style id='RCxIs'><dir id='RCxIs'><q id='RCxIs'></q></dir></style></legend>

                <bdo id='RCxIs'></bdo><ul id='RCxIs'></ul>
              • <i id='RCxIs'><tr id='RCxIs'><dt id='RCxIs'><q id='RCxIs'><span id='RCxIs'><b id='RCxIs'><form id='RCxIs'><ins id='RCxIs'></ins><ul id='RCxIs'></ul><sub id='RCxIs'></sub></form><legend id='RCxIs'></legend><bdo id='RCxIs'><pre id='RCxIs'><center id='RCxIs'></center></pre></bdo></b><th id='RCxIs'></th></span></q></dt></tr></i><div id='RCxIs'><tfoot id='RCxIs'></tfoot><dl id='RCxIs'><fieldset id='RCxIs'></fieldset></dl></div>

                本文介绍了为 QPrintWidget、QPrintPrevwiwWidget 添加页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我的发票

                我在 Python 中有一个几乎完成的项目,它使用 QPrintPreviwDialog 根据上图显示和打印数据.我使用 QTextDocumento 来处理 HTML.

                有没有办法在页码空间中写一些东西.我想将黄色信息写入页码空间作为页脚.或者是否有其他解决方案来显示页脚而不是使用不属于 python 的 pyjasper?

                解决方案

                My Invoice

                I have a almost finishd project in Python which uses QPrintPreviwDialog to show and print data according to the picture above. I have used QTextDocumento to handle the HTML.

                Is there a way to write something in page number space. I would like to write the info in yellow to the page number space to serve as footer. Or is there other solution to show footer instead of using pyjasper, one that are not part of python?

                解决方案

                Translating my previous answer from C++ to python and modifying the position of the text I show how to add a footer.

                from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
                
                textMargins = 12
                borderMargins = 10
                
                def mmToPixels(printer, mm):
                    return mm * 0.039370147 * printer.resolution()
                
                def paintPage(pageNumber, pageCount, painter, doc, textRect, footerHeight):
                    painter.save()
                    textPageRect = QtCore.QRectF(QtCore.QPointF(0, pageNumber*doc.pageSize().height()), doc.pageSize())
                    painter.setClipRect(textRect)
                    painter.translate(0, -textPageRect.top())
                    painter.translate(textRect.left(), textRect.top())
                    doc.drawContents(painter)
                    painter.restore()
                    footerRect = QtCore.QRectF(textRect)
                    footerRect.setTop(textRect.bottom())
                    footerRect.setHeight(footerHeight)
                
                    # draw footer
                    painter.save()
                    pen = painter.pen()
                    pen.setColor(QtCore.Qt.blue)
                    painter.setPen(pen)
                    painter.drawText(footerRect, QtCore.Qt.AlignCenter, "Page {} of {}".format(pageNumber+1, pageCount))
                    painter.restore()
                
                
                def printDocument(printer, doc):
                    painter = QtGui.QPainter(printer)
                    doc.documentLayout().setPaintDevice(printer)
                    doc.setPageSize(QtCore.QSizeF(printer.pageRect().size()))
                    pageSize = printer.pageRect().size()
                    tm = mmToPixels(printer, textMargins)
                    footerHeight = painter.fontMetrics().height()
                    textRect = QtCore.QRectF(tm, tm, pageSize.width() - 2 * tm, pageSize.height() - 2 * tm - footerHeight)
                    doc.setPageSize(textRect.size())
                    pageCount = doc.pageCount()
                
                    for pageIndex in range(pageCount):
                        if pageIndex != 0:
                            printer.newPage()
                        paintPage(pageIndex, pageCount, painter, doc, textRect, footerHeight)
                
                if __name__ == '__main__':
                    import sys
                
                    app = QtWidgets.QApplication(sys.argv)
                    document = QtGui.QTextDocument()
                    cursor = QtGui.QTextCursor(document)
                    blockFormat = QtGui.QTextBlockFormat()
                
                    for i in range(10):
                        cursor.insertBlock(blockFormat)
                        cursor.insertHtml("<h1>This is the {} page</h1>".format(i+1))
                        blockFormat.setPageBreakPolicy(QtGui.QTextFormat.PageBreak_AlwaysBefore)
                
                    printer = QtPrintSupport.QPrinter(QtPrintSupport.QPrinter.HighResolution)
                    printer.setPageSize(QtPrintSupport.QPrinter.A4)
                    printer.setOutputFormat(QtPrintSupport.QPrinter.PdfFormat)
                
                    dialog = QtPrintSupport.QPrintPreviewDialog(printer)
                    dialog.paintRequested.connect(lambda print, doc=document: printDocument(printer, doc))
                
                
                    dialog.exec_()
                

                这篇关于为 QPrintWidget、QPrintPrevwiwWidget 添加页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:PyQt5:样式表和来自 QWidget 的继承 下一篇:带有上下文菜单的 QTreeWidget,无法获取正确的项

                相关文章

                最新文章

                  <small id='AcJCo'></small><noframes id='AcJCo'>

                    <legend id='AcJCo'><style id='AcJCo'><dir id='AcJCo'><q id='AcJCo'></q></dir></style></legend><tfoot id='AcJCo'></tfoot>
                    • <bdo id='AcJCo'></bdo><ul id='AcJCo'></ul>
                  1. <i id='AcJCo'><tr id='AcJCo'><dt id='AcJCo'><q id='AcJCo'><span id='AcJCo'><b id='AcJCo'><form id='AcJCo'><ins id='AcJCo'></ins><ul id='AcJCo'></ul><sub id='AcJCo'></sub></form><legend id='AcJCo'></legend><bdo id='AcJCo'><pre id='AcJCo'><center id='AcJCo'></center></pre></bdo></b><th id='AcJCo'></th></span></q></dt></tr></i><div id='AcJCo'><tfoot id='AcJCo'></tfoot><dl id='AcJCo'><fieldset id='AcJCo'></fieldset></dl></div>