[PyQt] QWebEnginePage API for callbacks

Florian Bruhin me at the-compiler.org
Tue Jan 6 04:27:23 GMT 2015

* David Cortesi <davecortesi at gmail.com> [2015-01-05 19:59:13 -0800]:
> On Mon, Jan 5, 2015 at 3:26 PM, Andreas Pakulat <apaku at gmx.de> wrote:
> >
> > 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].
> >>
> >
> > I'd say just as all other callbacks in Qt (i.e. signal/slots), return
> > control to the qt event loop.
> >
> Could you clarify a bit? The doc [1] for QWebEnginePage.toPlainText() will
> at some unknown future time call my callback passing the text of the page.
> But I would make that call in some code that needs that text -- to write it
> to a file, say. So in a custom class based on QWebEngineView, I capture the
> ^S key, perhaps, for Save. I put a findSaveDialog and get a path string and
> open a text stream for output. Then...
>     self.page().toPlainText( lambda s : self.save_plain_text = s )
>     # Here --  twiddle my thumbs until I can...
>     output_stream << self.save_plain_text
> Is this the kind of thing you have in mind?
>     self.save_plain_text = None
>     self.page().toPlainText( lambda s : self.save_plain_text = s )
>     while self.save_plain_text is None : QCoreApplication.processEvents()
>     output_stream << self.save_plain_text

Probably something like this:

    def handle_ctrl_s(self):
        self.save_filename = get_filename_from_user()

    def do_save(self, data):
        with open(self.save_filename, 'w') as f:

Or this using functools.partial, i.e. it won't break if the user
presses Ctrl-S again before the text is ready:

    def handle_ctrl_s(self):
        filename = get_filename_from_user()
        self.page().toPlainText(partial(self.do_save, filename))

    def do_save(self, filename, data):
        with open(filename, 'w') as f:

I think this is a general concept which is very important to grasp
(and also took me a while) when programming with something like PyQt:

Try to make everything happen async - the majority of the time should
be spent in the Qt event loop (i.e. the app.exec_() call). Any code
you write gets called from that event loop usually, and should finish
"immediately". So here, you don't "wait" until the data is ready, you
tell Qt "call me back when the data is ready", return to the event
loop, and *then* save it when it *is* ready.


http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150106/61ac8adf/attachment-0001.sig>

More information about the PyQt mailing list