首页 > 解决方案 > Python Adding Pictures and headers to QPrintWidget

问题描述

How can I add a header and a picture to my QPrintWidget /Printout ?

I found this really nice looking layouted outputfile. I would love to make my file look somthing like that. I tried to find a tutorial explaining how to do the 'layouting' of a file, but could not find any.

I would be really glad about some suggestions/help.

So far this is mycode, where the table is beeing drawn from the QTableWidget:

 def handlePaintRequest(self, printer):
        document = QtGui.QTextDocument()
        cursor = QtGui.QTextCursor(document)

        table = cursor.insertTable(self.table.rowCount(), self.table.columnCount())

        for row in range(table.rows()):
            for col in range(table.columns()):
                it = self.table.item(row, col)
                if it is not None:
                    cursor.insertText(it.text())
                cursor.movePosition(QtGui.QTextCursor.NextCell)
        document.print_(printer)

enter image description here

标签: python-3.xpyqt5qtextdocumentqtextcursor

解决方案


我的答案基于您所指的上述链接....我只是向您展示如何添加标题..我没有更改当前的解决方案,只是给您一个可能的解决方案....

def paintPage(pageNumber, pageCount, painter, doc, textRect, footerHeight):

    .......................

    headerRect = QtCore.QRectF(textRect)
    headerRect.setTop(textRect.top())
    headerRect.setHeight(2*footerHeight)

    .......................

painter.drawText(footerRect, QtCore.Qt.AlignCenter, "Page {} of {}".format(pageNumber+1, pageCount))
painter.drawText(headerRect, QtCore.Qt.AlignLeft, "{}\n{}".format('Project name:', 'Project number:'))

推荐阅读