[PyQt] QWebEnginePage API for callbacks

Kovid Goyal kovid at kovidgoyal.net
Tue Jan 6 06:26:11 GMT 2015


There is no direct way to convert an asynchronous API into a synchronous
one.

Here is a hackish way to do it.

def save(self):
  text = None
  def finished(x):
    text = x
     
  self.view.toPlainText(finished)
  while text is None:
      QApplication.instance().processEvents(QEventLoop.ExcludeUserInputEvents | QEventLoop.ExcludeSocketNotifiers | QEventLoop.WaitForMoreEvents)
  # now save the text

This is the simplest (somewhat robust) way to simulate the old blocking
behavior of toPlainText(). There are some problems with this
approach:

Spinning the event loop means that other parts of your code might be
called, for instance in response to timer events, or events from the OS.
With the old synchronous API that could not happen.

However, for most use cases it will work fine.

What is really needed is for Qt to provide a synchronous wrapper around
the API that avoids these problems. In other words a flag to
processEvents that causes it to only deliver webkit related events and
hold everything else. That would make porting to
QWebEngine somewhat easier. 

Kovid.

On Mon, Jan 05, 2015 at 02:06:25PM -0800, David Cortesi wrote:
> In Qt5.4, the new QWebEngine interface takes account of the fact that the
> underlying Chromium engine is asynchronous. Four of the methods of
> QWebEnginePage take an argument "FunctorOrLambda" as a callback to receive
> control when the result is ready (for example, [1]).
> 
> Using PyQt, I assume we can pass a Python defined function or lambda in
> this position?
> 
> I'm a bit at a loss what to do following a call to such a method. What is
> the best way to "idle" until the callback comes? n.b. I don't see any
> WebEngine specific guidance at the PyQt5 doc [2].
> 
> Thanks for any suggestions,
> Dave Cortesi
> 
> [1] http://doc.qt.io/qt-5/qwebenginepage.html#toPlainText
> 
> [2] http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html
> 
> 
> !DSPAM:3,54ab105d17503335820283!

> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> 
> !DSPAM:3,54ab105d17503335820283!


-- 
_____________________________________

Dr. Kovid Goyal 
http://www.kovidgoyal.net
http://calibre-ebook.com
_____________________________________


More information about the PyQt mailing list