Subclass of QWebView doesn’t detect/react to Hyperlink Clicks



  • Hello

    This is in Python/PySide.

    I am trying to create my own Parental WebBrowser by overloading the PySide.QtWebKit.QWebView widget. Then whenever someone clicks a link on the widget I check to see if we are going to an invalid website, if not we proceed, if yes then I redirect to a generic page.

    So I have subclassed the PySide.QtWebKit.QWebView, but I am not receiving notification of when a link is clicked. I have overridden the linkClicked function but the function never runs when a link is clicked?

    What am I doing wrong? why cant my function run/react to the hyperlink click "event"? Do I need to override the webpage object & not this class to react to link clicks?

    @import PySide.QtWebKit
    import sys
    from PyQt4 import QtGui

    class BrowserWindow( PySide.QtWebKit.QWebView ):
    # Class Variables:

    def __init__( self, _parent ):
        """ Constructor: """
    
        super(BrowserWindow, self).__init__()
        PySide.QtWebKit.QWebView(None)
    
    
    def linkClicked(self, arg__1):
        """ Post: """
    
        #print("LINK CLICKED")
        #text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 
        #    'Enter your name:')
    
        self.load("http://yahoo.com")
    

    def main():

    app = QtGui.QApplication(sys.argv)
    
    view = BrowserWindow(None) #PySide.QtWebKit.QWebView(None)
    view.load("http://google.com")
    view.show()
    
    sys.exit(app.exec_())
    

    if name == 'main':
    main()

    @



  • Hey mate,

    For this to work you must first set the way links need to be handled.
    Default is that all links will be handled by QWebPage even if you made your own function.
    This is done with page().setLinkDelegationPolicy().

    Secondly your now overloading the signal that is send when someone clicks a link and not the slot.
    To get this to work you need to connect your own slot to the linkClicked signal.

    So try:

    @class BrowserWindow( PySide.QtWebKit.QWebView ):
    # Class Variables:

    def __init__( self, _parent ):
        """ Constructor: """
    
        super(BrowserWindow, self).__init__()
    
        self.page().setLinkDelegationPolicy(PySide.QtWebKit.QWebPage.DelegateAllLinks)
        self.linkClicked.connect(self.linkIsClicked)
    
    
    def linkIsClicked(self, arg__1):
        """ Post: """
    
        #print("LINK CLICKED")
        #text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 
        #    'Enter your name:')
    
        self.load("http://yahoo.com")@
    

    With setLinkDelegationPolicy you have three options:

    • QWebPage.DontDelegateLinks -> No links are delegated. Instead, QWebPage tries to handle them all.

    • QWebPage.DelegateExternalLinks -> When activating links that point to documents not stored on the local filesystem or an equivalent - such as the Qt resource system - then linkClicked() is emitted.

    • QWebPage.DelegateAllLinks -> Whenever a link is activated the linkClicked() signal is emitted.

    Hope this helps :).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.