更改或删除QPrintPreviewWidget中的页码编号

时间:2018-03-02 06:13:04

标签: python pyqt print-preview page-numbering

是否可以删除/更改QPrintPreviewWidget中的页面编号?我试图通过将页边距设置为(0,10,0,3)来“删除”它。在打印预览对话框中,它看起来很好:页码超出了页面边框。但是当我打印页面时,仍然有页码。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys

html_text = """
        <html><head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
        <body>
            <p><span style=" font-size:20pt; font-weight:600;">QTextEdit</span></p>
        </body></html>
        """

class Preview(QtGui.QMainWindow):
    def __init__(self):
        super(Preview, self).__init__()

        self.textedit = QtGui.QTextEdit()
        codec = QtCore.QTextCodec.codecForHtml(html_text)
        unucode_string = codec.toUnicode(html_text)
        self.textedit.setHtml(unucode_string)    

        self.preview_widget = QtGui.QPrintPreviewWidget(self)
        self.preview_widget.resize(1100, 800)
        self.preview_widget.move(50, 50)
        self.preview_widget.paintRequested.connect(self.print_preview)
        self.preview_widget.updatePreview()

        self.resize(1200, 820)
        self.show()


    def print_preview(self, printer):
        printer.setOrientation(1)
        printer.setPageSize(8)

        # this is my attempt to solve problem:
        # printer.setPageMargins(0, 10, 0, 3, 0)

        self.textedit.print_(printer)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    mainWindows = []
    for fn in sys.argv[1:] or [None]:
        preview = Preview()
        preview.show()
        mainWindows.append(preview)

    sys.exit(app.exec_())

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试设置文档的页面大小。以下是基于您的示例的演示。 (请注意,如果您使用文本小部件来保存html,则必须显示它,否则页面大小调整将无法正常工作。)

import sys
from PyQt4 import QtGui, QtCore

html = """<html><body>%s</body></html>""" % """
<p><span style=" font-size:20pt; font-weight:600;">QTextEdit</span></p>
""" * 20

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.document = QtGui.QTextDocument(self)
        self.document.setHtml(html)
        self.preview = QtGui.QPrintPreviewWidget(self)
        self.preview.paintRequested.connect(self.handlePaintRequest)
        self.setCentralWidget(self.preview)

    def handlePaintRequest(self, printer):
        printer.setOrientation(QtGui.QPrinter.Landscape)
        printer.setPageSize(QtGui.QPrinter.A3)
        self.document.setPageSize(
            QtCore.QSizeF(printer.width(), printer.height()))
        self.document.print_(printer)

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 1200, 800)
    window.show()
    sys.exit(app.exec_())