简短版
我在窗口小部件中嵌入了一个简单的QTreeView
:
我想将树保存为PDF,以便用户可以打印它。我怎样才能做到这一点?
详情
我想在点击“打印”时将树保存为PDF格式。按钮。基于this question,有点基于Qt documentation,我尝试使用以下方法(请参阅下面的完整SSCCE以获取上下文):
def print_(self):
printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
printer.setPageSize(QtGui.QPrinter.Letter)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
printer.setOutputFileName("treeTest.pdf")
painter = QtGui.QPainter()
painter.begin(printer)
self.view.render(painter)
painter.end()
但是当运行此方法时,我收到错误:
TypeError: 'PySide.QtGui.QWidget.render' called with wrong argument types.
PySide.QtGui.QWidget.render(PySide.QtGui.QPainter)
奇怪的是,the documentation for render将第一个参数作为QPainter
类型,所以我不确定为什么我的代码不起作用,特别是对类型错误感到困惑。
我是否遗漏了一些简单的双线程,让我将我的树导出为PDF?
SSCCE
from PySide import QtGui, QtCore
import sys
class MyTreeView(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent = None)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.view = QtGui.QTreeView(self)
self.createModel()
self.view.setModel(self.model)
buttonLayout = self.buttonSetup()
self.makeConnections()
mainLayout = QtGui.QHBoxLayout(self)
mainLayout.addWidget(self.view)
mainLayout.addLayout(buttonLayout)
self.setLayout(mainLayout)
self.view.expandAll()
def createModel(self):
self.model = QtGui.QStandardItemModel()
self.model.setHorizontalHeaderLabels(['Task', 'Comment'])
self.rootItem = self.model.invisibleRootItem()
#First top-level row and children
item0 = [QtGui.QStandardItem('Coding fun'), QtGui.QStandardItem('Make millions')]
item00 = [QtGui.QStandardItem('Write example'), QtGui.QStandardItem('Keep it simple')]
item01 = [QtGui.QStandardItem('Post to SO'), QtGui.QStandardItem('Put on flame-retardant vest')]
item00[0].setCheckable(True)
item00[0].setCheckState(QtCore.Qt.Unchecked)
item01[0].setCheckable(True)
item01[0].setCheckState(QtCore.Qt.Unchecked)
self.rootItem.appendRow(item0)
item0[0].appendRow(item00)
item0[0].appendRow(item01)
def makeConnections(self):
self.printButton.clicked.connect(self.print_)
self.quitButton.clicked.connect(self.close)
def print_(self):
printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
printer.setPageSize(QtGui.QPrinter.Letter)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
printer.setOutputFileName("treeTest.pdf")
painter = QtGui.QPainter()
painter.begin(printer)
self.view.render(painter)
painter.end()
def buttonSetup(self):
self.printButton = QtGui.QPushButton("Print")
self.quitButton = QtGui.QPushButton("Quit")
#Lay them out
buttonLayout = QtGui.QVBoxLayout()
buttonLayout.addStretch()
buttonLayout.addWidget(self.printButton)
buttonLayout.addStretch()
buttonLayout.addWidget(self.quitButton)
return buttonLayout
def main():
app = QtGui.QApplication(sys.argv)
newTree = MyTreeView()
newTree.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
答案 0 :(得分:2)
根据docs,QWidget.render
作为第一个参数QPainter
的变体需要targetOffset
类型的QPoint
作为第二个参数参数。如果我添加一个空点它对我有效(虽然树小部件打印得非常小)。
painter.begin(printer)
try:
self.view.render(painter, QtCore.QPoint())
finally:
painter.end()