Printing from QtWebKit

403 views
Skip to first unread message

Andy

unread,
Nov 23, 2012, 10:39:16 AM11/23/12
to pyqt-...@googlegroups.com
Hi guys!

I made a QtWebKit based web browser which now has to be able to support printing and downloading of files.  Both of these fail in a similar way, and in this thread perhaps I'll focus first on printing.

The printing is triggered from the html file with some Javascript code like so:  onClick="window.print()"

When I open the page with Google Chrome and click on the link, the print setup window pops up as expected and I can proceed printing.  But when I open the same page with my custom browser, the print setup window does not appear.  (Same problem with the "save as" window).

After much reading and testing, I came to the conclusion that it's necessary to subclass the QWebPage class and reimplement the printRequested function.  My CustomWebPage class looks like this:

class CustomWebPage(QtWebKit.QWebPage):
    def customPrintRequested(self, webFrame):
        print "printRequested"
        self.printer = QtGui.QPrinter()
        dialog = QtGui.QPrintDialog()
        dialog.setWindowTitle(tr("Print Document"))
        if dialog.show() != QtGui.QDialog.Accepted:
            return
        print "now printing!"
        webFrame.print()
        
    def __init__(self, parent = None):
print("CustomWebPage")
QtWebKit.QWebPage.__init__(self, parent)
QtCore.QObject.connect(self, QtCore.SIGNAL("printRequested()"), self.customPrintRequested)

In another part of my code I go assembling my web browser and set the webview to use this custom webpage with:  self.webView.setPage(CustomWebPage())

Can somebody please give me a clue as to what is it that I'm doing wrong?  I know there are at least two wrong things here:
  1. The way I'm attempting to define the event (QObject.connect).
  2. The way I'm trying to do the webFrame.print()
I'm using 64 bit Windows 7, with Python 2.6 and PyQt 4.9.5

Any suggestions?
Thanks!
Andy
sample.html
qtwebkitprint.py

zw g

unread,
Nov 26, 2012, 5:30:11 AM11/26/12
to Andy, pyqt-pyside
Why not speak Chinese? You're not a Chinese?


Running the file you attached gives the below error:

$ python qtwebkitprint.py 
  File "qtwebkitprint.py", line 28
    webFrame.print()
                 ^
SyntaxError: invalid syntax

Gui


Andy

--
PyQt & PySide的交流信息:
 
QQ群号: 143061743
邮件列表: pyqt-...@googlegroups.com
---
您收到此邮件是因为您订阅了 Google 网上论坛的“PyQt & PySide”论坛。
要向此网上论坛发帖,请发送电子邮件至 pyqt-...@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 pyqt-pyside...@googlegroups.com
通过以下网址访问此论坛:http://groups.google.com/group/pyqt-pyside?hl=zh-CN。
 
 

Andy

unread,
Nov 26, 2012, 6:11:48 AM11/26/12
to pyqt-...@googlegroups.com, Andy
Thank you zw,

I do not speak Chinese.

That's right, as I said in my original post, that's one of the problem lines.

Andy

zw g

unread,
Nov 26, 2012, 9:15:17 AM11/26/12
to Andy, pyqt-pyside
OK. I see. Never mind.



How about this for the connect part?

self.connect(self, QtCore.SIGNAL("printRequested()"), self.customPrintRequested)


And from the below page, it shows you the usage of the print method of QWebframe:


==========================================

QWebFrame.print (selfQPrinter printer)

This method is also a Qt slot with the C++ signature void print(QPrinter *) const.

Prints the frame to the given printer.

See also render().

==========================================

From above you need pass the QPrinter object to it.

I didn't try that yet.  You may need give it a try first.



Gui

zw g

unread,
Nov 26, 2012, 9:50:13 AM11/26/12
to Andy, pyqt-pyside
Andy,



I know where is the problem now, change the code like below, it works on my Macbook:


# you have no tr() or sth imported. So:
dialog.setWindowTitle("Print Document")


# print() -->  print_(self.printer)
webFrame.print_(self.printer)


# printRequested()  -->  printRequested(QWebFrame *)
self.connect(self, QtCore.SIGNAL("printRequested(QWebFrame *)"), self.customPrintRequested)


Hope it helps.


Gui

Gui

Andy

unread,
Nov 26, 2012, 5:33:32 PM11/26/12
to pyqt-...@googlegroups.com, Andy
Dear zw,

You are a genius!  Thank you so much!

So for the people who might find this useful, this is the final shape of the custom QWebPage class:

class CustomWebPage(QtWebKit.QWebPage):
    def customPrintRequested(self, webFrame):
        self.printer = QtGui.QPrinter()
        dialog = QtGui.QPrintDialog()
        dialog.setWindowTitle("Print Document")
        if dialog.show() != QtGui.QDialog.Accepted:
            return
        webFrame.print_(self.printer)
        
    def __init__(self, parent = None):
print("CustomWebPage")
QtWebKit.QWebPage.__init__(self, parent)
        self.connect(self, QtCore.SIGNAL("printRequested(QWebFrame *)"), self.customPrintRequested)

... and don't forget to configure your QWebView to use this custom class with:

self.webView.setPage(CustomWebPage())

Cheers from Lima, Peru.
Andy


On Monday, November 26, 2012 9:50:35 AM UTC-5, zw g wrote:
Andy,



I know where is the problem now, change the code like below, it works on my Macbook:


# you have no tr() or sth imported. So:
dialog.setWindowTitle("Print Document")


# print() -->  print_(self.printer)
webFrame.print_(self.printer)


# printRequested()  -->  printRequested(QWebFrame *)
self.connect(self, QtCore.SIGNAL("printRequested(QWebFrame *)"), self.customPrintRequested)


Hope it helps.


Gui

Gui
Reply all
Reply to author
Forward
0 new messages