[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] How to customize link click behavier of QWebEngine?
From:       "Jones, Bryan" <bjones () ece ! msstate ! edu>
Date:       2018-07-17 14:33:29
Message-ID: CAHhYbD-47uJ9k5H3hQkuAgSEw6AHVj5BGJeCBcYb0twrEQze4w () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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@gmail.com> wrote:

>
>
> On 17 July 2018 at 15:05, Andy Stewart <lazycat.manatee@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@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)

[Attachment #5 (text/html)]

<div dir="ltr">Here&#39;s what I do -- override the acceptNavigationRequest method in \
a derived class.<div><br></div><div> <p style="margin:0px;white-space:pre-wrap">class \
QWebEnginePageExtLink(QWebEnginePage):</p> <p \
style="margin:0px;white-space:pre-wrap">    def acceptNavigationRequest(self, url, \
navigationType, isMainFrame):</p> <p style="margin:0px;white-space:pre-wrap">        \
# Only open a link externally if the user clicked on it.</p> <p \
style="margin:0px;white-space:pre-wrap">        #</p> <p \
style="margin:0px;white-space:pre-wrap">        # The following HTML produces \
navigationType == 0 (link clicked) and</p> <p \
style="margin:0px;white-space:pre-wrap">        # isMainFrame == False. (This makes \
no sense to me). So, only open main frame clicks  in an external browser.</p> <p \
style="margin:0px;white-space:pre-wrap">        ## &lt;a class=&quot;reference \
external image-reference&quot; href=&quot;<a \
href="https://pypi.python.org/pypi/PyInstaller">https://pypi.python.org/pypi/PyInstaller</a>&quot;&gt;&lt;object \
data=&quot;<a href="https://img.shields.io/pypi/v/PyInstaller.svg">https://img.shields.io/pypi/v/PyInstaller.svg</a>&quot; \
type=&quot;image/svg+xml&quot;&gt;<a \
href="https://img.shields.io/pypi/v/PyInstaller.svg">https://img.shields.io/pypi/v/PyInstaller.svg</a>&lt;/object&gt;&lt;/a&gt;</p>
 <p style="margin:0px;white-space:pre-wrap">        if (navigationType == \
QWebEnginePage.NavigationTypeLinkClicked and isMainFrame):</p> <p \
style="margin:0px;white-space:pre-wrap">            res = \
QDesktopServices.openUrl(url)</p> <p style="margin:0px;white-space:pre-wrap">         \
if res:</p> <p style="margin:0px;white-space:pre-wrap">                \
core.mainWindow().statusBar().showMessage(&quot;{} opened in a \
browser&quot;.format(url.toString()), 2000)</p> <p \
style="margin:0px;white-space:pre-wrap">            else:</p> <p \
style="margin:0px;white-space:pre-wrap">                \
core.mainWindow().statusBar().showMessage(&quot;Failed to open \
{}&quot;.format(url.toString()), 2000)</p> <p \
style="margin:0px;white-space:pre-wrap"><br></p> <p \
style="margin:0px;white-space:pre-wrap">            # Tell the built-in browser not \
to handle this.</p> <p style="margin:0px;white-space:pre-wrap">            return \
False</p> <p style="margin:0px;white-space:pre-wrap">        else:</p>
<p style="margin:0px;white-space:pre-wrap">            # Handle this in the built-in \
browser.</p> <p style="margin:0px;white-space:pre-wrap">            return True</p>
<p style="margin:0px;white-space:pre-wrap"><br></p><br></div></div><br><div \
class="gmail_quote"><div dir="ltr">On Tue, Jul 17, 2018 at 9:28 AM J Barchan &lt;<a \
href="mailto:jnbarchan@gmail.com">jnbarchan@gmail.com</a>&gt; \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 \
.8ex;border-left:1px #ccc solid;padding-left:1ex">



<div>
<div dir="ltr">
<div class="gmail_default" style="font-family:tahoma,sans-serif"><br>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On 17 July 2018 at 15:05, Andy Stewart <span \
dir="ltr">&lt;<a href="mailto:lazycat.manatee@gmail.com" \
target="_blank">lazycat.manatee@gmail.com</a>&gt;</span> wrote:<br> <blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"> <div dir="ltr">
<div style="font-family:simhei,sans-serif">Hi guys.</div>
<div style="font-family:simhei,sans-serif"><br>
</div>
<div style="font-family:simhei,sans-serif">In QWebKit, I just use need customize \
function QWebPage:acceptNavigationRequest to change link click behaver:</div> <div \
style="font-family:simhei,sans-serif"><br> </div>
<div style="font-family:simhei,sans-serif">1. Click   to open link in current \
tab</div> <div style="font-family:simhei,sans-serif">2. Tab  + Click to open link in \
new tab</div> <div style="font-family:simhei,sans-serif"><br>
</div>
<div style="font-family:simhei,sans-serif">like below code:</div>
<div style="font-family:simhei,sans-serif"><br>
</div>
<div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid \
rgb(204,204,204);padding-left:1ex"> <font face="simhei, sans-serif">class \
WebPage(QWebPage):</font><font face="simhei, sans-serif"><br> </font><font \
face="simhei, sans-serif">      open_url_in_new_tab = \
QtCore.pyqtSignal(str)</font><font face="simhei, sans-serif"><br> </font><font \
face="simhei, sans-serif">      def __init__(self):<br> </font><font face="simhei, \
sans-serif">            super(WebPage, self).__init__()</font><font face="simhei, \
sans-serif"><br> </font><font face="simhei, sans-serif">      def \
acceptNavigationRequest(self, frame, request, type):<br> </font><font face="simhei, \
sans-serif">            modifiers = QApplication.keyboardModifiers()</font><font \
face="simhei, sans-serif"><br> </font><font face="simhei, sans-serif">            # \
Handle myself if got user event.<br> </font><font face="simhei, sans-serif">          \
if type == QWebPage.NavigationTypeLinkClicked:<br> </font><font face="simhei, \
sans-serif">                  if modifiers == Qt.ControlModifier:<br> </font><font \
face="simhei, sans-serif">                        \
self.open_url_in_new_tab.emit(request.url().toString())<br> </font><font \
face="simhei, sans-serif">                  else:<br> </font><font face="simhei, \
sans-serif">                        self.view().load(request.url())</font><font \
face="simhei, sans-serif"><br> </font><font face="simhei, sans-serif">                \
# Return False to stop default behavior.<br> </font><font face="simhei, sans-serif">  \
return False</font><font face="simhei, sans-serif"><br> </font><font face="simhei, \
sans-serif">            # # Otherwise, use default behavior.<br> </font><font \
face="simhei, sans-serif">            return QWebPage.acceptNavigationRequest(self, \
frame, request, type)</font></blockquote> <div \
style="font-family:simhei,sans-serif"><br> </div>
<div style="font-family:simhei,sans-serif">My question is, how customize click \
behavior in QWebEngine?</div> <div style="font-family:simhei,sans-serif"><br>
</div>
<div style="font-family:simhei,sans-serif">I have search Google and StackOverflow, \
noting can help.</div> <div style="font-family:simhei,sans-serif"><br>
</div>
<div style="font-family:simhei,sans-serif">Thanks for your help!</div>
<span class="m_3975476431920920338HOEnZb"><font color="#888888">
<div style="font-family:simhei,sans-serif"><br>
</div>
<div style="font-family:simhei,sans-serif">  -- Andy</div>
</font></span></div>
<div style="font-family:simhei,sans-serif">  </div>
</div>
<br>
_______________________________________________<br>
PyQt mailing list      <a href="mailto:PyQt@riverbankcomputing.com" \
target="_blank">PyQt@riverbankcomputing.com</a><br> <a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noreferrer" \
target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br> \
</blockquote> </div>
<br>
<div style="font-family:tahoma,sans-serif" class="gmail_default">​</div>
<div style="font-family:tahoma,sans-serif" class="gmail_default">I stand to be \
corrected, but I do not believe ​you can do this any longer when you move from \
`QWebKit` to `QWebEngine`....<br> </div>
<br clear="all">
<br>
-- <br>
<div class="m_3975476431920920338gmail_signature" data-smartmail="gmail_signature">
<div dir="ltr">
<div>
<div dir="ltr">
<div><span style="font-family:tahoma,sans-serif">Kindest,</span></div>
<div><span style="font-family:tahoma,sans-serif">Jonathan</span></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" \
class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Bryan A. \
Jones, Ph.D.<br>Associate Professor<br>Department of Electrical and Computer \
Engineering<br>231 Simrall / PO Box 9571<br>Mississippi State \
University<br>Mississippi State, MS 39762<br><a \
href="http://www.ece.msstate.edu/~bjones" \
target="_blank">http://www.ece.msstate.edu/~bjones</a><br>bjones AT ece DOT msstate \
DOT edu<br>voice 662-325-3149<br>fax 662-325-2298<br><br>Our Master, Jesus Christ, is \
on his way. He&#39;ll show up right on<br>time, his arrival guaranteed by the Blessed \
and Undisputed Ruler,<br>High King, High God.<br>- 1 Tim. 6:14b-15 (The \
Message)<br></div></div></div>


[Attachment #6 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic