[PyQt] How to customize link click behavier of QWebEngine?

Jones, Bryan bjones at ece.msstate.edu
Tue Jul 17 15:33:29 BST 2018


Here's what I do -- override the acceptNavigationRequest method in a
derived class.

class QWebEnginePageExtLink(QWebEnginePage):

def acceptNavigationRequest(self, url, navigationType, isMainFrame):

# Only open a link externally if the user clicked on it.

#

# The following HTML produces navigationType == 0 (link clicked) and

# isMainFrame == False. (This makes no sense to me). So, only open main
frame clicks in an external browser.

## <a class="reference external image-reference" href="
https://pypi.python.org/pypi/PyInstaller"><object data="
https://img.shields.io/pypi/v/PyInstaller.svg" type="image/svg+xml">
https://img.shields.io/pypi/v/PyInstaller.svg</object></a>

if (navigationType == QWebEnginePage.NavigationTypeLinkClicked and
isMainFrame):

res = QDesktopServices.openUrl(url)

if res:

core.mainWindow().statusBar().showMessage("{} opened in a
browser".format(url.toString()), 2000)

else:

core.mainWindow().statusBar().showMessage("Failed to open
{}".format(url.toString()), 2000)


# Tell the built-in browser not to handle this.

return False

else:

# Handle this in the built-in browser.

return True




On Tue, Jul 17, 2018 at 9:28 AM J Barchan <jnbarchan at gmail.com> wrote:

>
>
> On 17 July 2018 at 15:05, Andy Stewart <lazycat.manatee at gmail.com> wrote:
>
>> Hi guys.
>>
>> In QWebKit, I just use need customize function
>> QWebPage:acceptNavigationRequest to change link click behaver:
>>
>> 1. Click  to open link in current tab
>> 2. Tab + Click to open link in new tab
>>
>> like below code:
>>
>> class WebPage(QWebPage):
>>>     open_url_in_new_tab = QtCore.pyqtSignal(str)
>>>     def __init__(self):
>>>         super(WebPage, self).__init__()
>>>     def acceptNavigationRequest(self, frame, request, type):
>>>         modifiers = QApplication.keyboardModifiers()
>>>         # Handle myself if got user event.
>>>         if type == QWebPage.NavigationTypeLinkClicked:
>>>             if modifiers == Qt.ControlModifier:
>>>                 self.open_url_in_new_tab.emit(request.url().toString())
>>>             else:
>>>                 self.view().load(request.url())
>>>             # Return False to stop default behavior.
>>>             return False
>>>         # # Otherwise, use default behavior.
>>>         return QWebPage.acceptNavigationRequest(self, frame, request,
>>> type)
>>
>>
>> My question is, how customize click behavior in QWebEngine?
>>
>> I have search Google and StackOverflow, noting can help.
>>
>> Thanks for your help!
>>
>>  -- Andy
>>
>>
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>
>> I stand to be corrected, but I do not believe ​you can do this any longer
> when you move from `QWebKit` to `QWebEngine`....
>
>
> --
> Kindest,
> Jonathan
>


-- 
Bryan A. Jones, Ph.D.
Associate Professor
Department of Electrical and Computer Engineering
231 Simrall / PO Box 9571
Mississippi State University
Mississippi State, MS 39762
http://www.ece.msstate.edu/~bjones
bjones AT ece DOT msstate DOT edu
voice 662-325-3149
fax 662-325-2298

Our Master, Jesus Christ, is on his way. He'll show up right on
time, his arrival guaranteed by the Blessed and Undisputed Ruler,
High King, High God.
- 1 Tim. 6:14b-15 (The Message)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180717/add97987/attachment-0001.html>


More information about the PyQt mailing list