0

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

4

1 回答 1

1

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

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:'))
于 2018-12-08T22:37:55.543 回答