QPrinter不会多次打印
问题描述:
在我的应用程序中,我有一种方法可让用户将报告转换为PDF文档。这完美 - 一次。如果用户再次点击该按钮,则转换会挂起。QPrinter不会多次打印
这是我的代码:
def print_report(self):
web = QtWebKit.QWebView()
filename = "reporttemplate.html"
file = open(filename,'r')
html = file.read()
file.close()
web.setHtml(html)
#web.show()
printer = QtGui.QPrinter()
printer.setPageSize(QtGui.QPrinter.Letter)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
# ---- BROKEN ----
# This next line is where it hangs on the second call to this function.
# The first time it works, and generates the PDF as expected.
# ---- BROKEN ON THE NEXT LINE! ----
printer.setOutputFileName(r'C:\path\to\report\directory\file.pdf')
def convertIt():
web.print_(printer)
print "Pdf generated"
web.close()
QtCore.QObject.connect(web, QtCore.SIGNAL("loadFinished(bool)"), convertIt)
我的想法是,打印机仍有打开该文件。如果是这样的话,我该如何关闭文件?
如果我重新启动应用程序并且文件已经存在,它就可以工作。出于这个原因,我不相信它是挂起的,因为文件已经存在。
答
测试你的代码我注意到对我来说,它只有当我把web.setHtml(html)
放在print_report
方法的末尾(最后一个语句)时才起作用。这样做,我可以根据需要多次生成file.pdf。
工作。谢谢。 – Andy 2012-07-23 14:57:27