[PyQt] Printing from QWebView (contents visible) gives a set of blank pages

ivanko.rus ivanko.rus at gmail.com
Sun Aug 21 18:52:41 BST 2011


Good afternoon! Recently I was trying to print a Web page from QWebView and
ran into a pretty weird problem. It is the following: I can print my
QWebView perfectly when setting the QPrinter's output format to PDF or
PostScript. But when I try to print to a normal printer, however, it gives
me the correct number of pages, only that the pages are all blank. I tried
to do this with a virtual PDF printer in Windows, a normal printer (it had
ink, btw =), tried to choose the "print to file" option on Windows, and the
results were all the same. The other thing that puzzles me even more is that
if I replace the QWebView with a QTextEdit, everything works perfectly with
all kinds of printers. So I hope that someone can give me a little help, I
would appreciate it very much. Thank you!

P.S. Here is the code that I use for testing that issue (I copied it from
http://orangepalantir.org/topicspace/index.php?idnum=31):

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import PyQt4.QtWebKit as QtWebKit

import sys

class htmlViewer(QtWebKit.QWebView):
    def __init__(self,url, parent=None):
        QtWebKit.QWebView.__init__(self,parent)
        self.setUrl(QUrl(url))
        self.preview = QPrintPreviewDialog()
        self.connect(self.preview,SIGNAL("paintRequested (QPrinter
*)"),SLOT("print (QPrinter *)"))
        self.connect(self,SIGNAL("loadFinished (bool)"),self.execpreview)
    def execpreview(self,arg):
        print arg
        self.preview.exec_()

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

    myurl = "http://www.google.com"
    widget = htmlViewer(myurl)
    widget.show()

    sys.exit(app.exec_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110821/47cf22a4/attachment.html>


More information about the PyQt mailing list