更改或删除 QPrintPreviewWidget 中的页码
Change or remove page numbering in QPrintPreviewWidget
QPrintPreviewWidget 中的页码可以delete/change吗?我试图通过将页边距设置为 (0, 10, 0, 3) 来 "delete" 它。在打印预览对话框中,它看起来不错:页码超出了页面边框。但是当我打印页面时,仍然有页码。
#!/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_())
尝试设置文档的 page-size。以下是基于您的示例的演示。 (请注意,如果您使用 text-widget 来容纳 html,则您 必须 显示它,否则 page-resizing 将无法正常工作)。 =11=]
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_())
QPrintPreviewWidget 中的页码可以delete/change吗?我试图通过将页边距设置为 (0, 10, 0, 3) 来 "delete" 它。在打印预览对话框中,它看起来不错:页码超出了页面边框。但是当我打印页面时,仍然有页码。
#!/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_())
尝试设置文档的 page-size。以下是基于您的示例的演示。 (请注意,如果您使用 text-widget 来容纳 html,则您 必须 显示它,否则 page-resizing 将无法正常工作)。 =11=]
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_())