[PyQt] Need help with PyQt and QWebChannel

Detlev Offenbach detlev at die-offenbachs.de
Wed Mar 16 18:07:50 GMT 2016


Hi,

I have to answer my own question. Maybe somebody else might face this in the future as 
well.

The issue I had was on the JavaScript side. Because searchUrl() returns something the 
functioncall in JavaScript has to define a callback function. This is needed because 
QWebChannel delivers the result from the Python/C++ side asynchronously.

Detlev

On Tuesday 15 March 2016, 21:14:42 Detlev Offenbach wrote:
> Hi,
> 
> I am in the process of porting the eric web browser from QtWebKit to
> QtWebEngine. In this process I am facing an issue I would need some help to
> resolve it.
> 
> I have a web page that contains a JavaScript part like:
> 
>     <script type="text/javascript">
>         function update()
>         {
>             alert(window.external.searchUrl());
>             document.getElementById('footer').innerHTML =
> window.external.providerString() + ' | <a
> href="http://eric-ide.python-projects.org/">' + '@ERIC_LINK@</a>';
> document.getElementById('lineEdit').placeholder =
> window.external.providerString();
> 
>             // Try to change the direction of the page:
> 
>             var newDir = '@QT_LAYOUT_DIRECTION@';
>             newDir = newDir.toLowerCase();
>             if ((newDir != 'ltr') && (newDir != 'rtl'))
>                 newDir = 'ltr';
>             document.getElementsByTagName('body')[0].setAttribute('dir',
> newDir); }
> 
>         function formSubmitted()
>         {
>             var string = lineEdit.value;
> 
>             if (string.length == 0)
>                 return;
> 
>             var url = window.external.searchUrl(string);
>             window.location.href = url;
>         }
> 
>         // Initialize
>         if (window.external) {
>             update();
>         } else {
>             document.addEventListener('_eric_external_created', update);
>         }
>     </script>
> 
> The "window.external.searchUrl" method is implemented in an object
> registered with a QWebChannel like
> 
>     @pyqtSlot(str, result=str)
>     def searchUrl(self, searchStr):
> 
> However, the alert() call in the above script always returns 'undefined',
> that is the function 'searchUrl' is not registered with it.
> 
> What am I doing wrong?
> 
> Detlev
> 
> PS: Full code can be retrieved from the eric repository at
> http://die-offenbachs.homelinux.org:48888/hg/eric/-- *Detlev Offenbach*
> detlev at die-offenbachs.de-- 
*Detlev Offenbach*
detlev at die-offenbachs.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160316/42ecb74f/attachment-0001.html>


More information about the PyQt mailing list