[PyQt] HELP with QtWebkit.

David Boddie david at boddie.org.uk
Wed Apr 28 23:39:48 BST 2010

On Wed, 28 Apr 2010 16:04:44 -0400, patx wrote:
> I have the code
> http://guest:guest@patx.me/hg/fastpatx/file/6e5ce46a49f9/fastpatx.py i need
> to know how i can make function that will allow users to right click a link
> and then open the link in a new tab... can someone please show me how to do
> this. i am stumped... (feel free to paste code at a popular pastebin site).

I guess you want to suppress the default context menu behaviour, as well as
add special behaviour for right clicks.

This simple example class suppresses the context menu by overriding the
default contextMenuEvent() handler in QWebView.

It also provides an implementation of mousePressEvent() that gets the URL
for any link the user clicks on - it checks if the URL is valid by calling
its isValid() method.

class WebView(QWebView):

    def mousePressEvent(self, event):
        url = self.page().currentFrame().hitTestContent(event.pos()).linkUrl()
        if url.isValid(): print url.toString()

    def contextMenuEvent(self, event):

Of course, you need to subclass QWebView if you take this approach. There
are other ways to catch clicks on the widget, but they are not as simple
as subclassing QWebView.


More information about the PyQt mailing list