[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: [PyQt] QWebEnginePage API for callbacks
From: Kevin Mcintyre <kebin70 () gmail ! com>
Date: 2015-01-06 5:44:41
Message-ID: CAA-sNEYXd7Esy2j60G9WNHaE5_e63z8n=fKYKTXBbEH5Y267ug () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
I too am curious, my software uses QWebElement extensively, and was a
little surprised how page visibility there is available.
On Mon, Jan 5, 2015 at 8:27 PM, Florian Bruhin <me@the-compiler.org> wrote:
> * David Cortesi <davecortesi@gmail.com> [2015-01-05 19:59:13 -0800]:
> > On Mon, Jan 5, 2015 at 3:26 PM, Andreas Pakulat <apaku@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()
> self.page().toPlainText(self.do_save)
>
> def do_save(self, data):
> with open(self.save_filename, 'w') as f:
> f.write(data)
>
> 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:
> f.write(data)
>
> 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.
>
> Florian
>
> --
> http://www.the-compiler.org | me@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/
>
> _______________________________________________
> PyQt mailing list PyQt@riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
[Attachment #5 (text/html)]
<div dir="ltr">I too am curious, my software uses QWebElement extensively, and was a \
little surprised how page visibility there is available.<br></div><div \
class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 5, 2015 at 8:27 PM, \
Florian Bruhin <span dir="ltr"><<a href="mailto:me@the-compiler.org" \
target="_blank">me@the-compiler.org</a>></span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">* David Cortesi <<a \
href="mailto:davecortesi@gmail.com">davecortesi@gmail.com</a>> [2015-01-05 \
19:59:13 -0800]:<br> <div><div class="h5">> On Mon, Jan 5, 2015 at 3:26 PM, \
Andreas Pakulat <<a href="mailto:apaku@gmx.de">apaku@gmx.de</a>> wrote:<br> \
><br> > ><br>
> > I'm a bit at a loss what to do following a call to such a method. What \
is<br> > >> the best way to "idle" until the callback comes? n.b. \
I don't see any<br> > >> WebEngine specific guidance at the PyQt5 doc \
[2].<br> > >><br>
> ><br>
> > I'd say just as all other callbacks in Qt (i.e. signal/slots), \
return<br> > > control to the qt event loop.<br>
> ><br>
><br>
> Could you clarify a bit? The doc [1] for QWebEnginePage.toPlainText() will<br>
> at some unknown future time call my callback passing the text of the page.<br>
> But I would make that call in some code that needs that text -- to write it<br>
> to a file, say. So in a custom class based on QWebEngineView, I capture the<br>
> ^S key, perhaps, for Save. I put a findSaveDialog and get a path string and<br>
> open a text stream for output. Then...<br>
><br>
> self.page().toPlainText( lambda s : self.save_plain_text = s )<br>
> # Here -- twiddle my thumbs until I can...<br>
> output_stream << self.save_plain_text<br>
><br>
> Is this the kind of thing you have in mind?<br>
><br>
> self.save_plain_text = None<br>
> self.page().toPlainText( lambda s : self.save_plain_text = s )<br>
> while self.save_plain_text is None : QCoreApplication.processEvents()<br>
> output_stream << self.save_plain_text<br>
<br>
</div></div>Probably something like this:<br>
<br>
def handle_ctrl_s(self):<br>
self.save_filename = get_filename_from_user()<br>
self.page().toPlainText(self.do_save)<br>
<br>
def do_save(self, data):<br>
with open(self.save_filename, 'w') as f:<br>
f.write(data)<br>
<br>
Or this using functools.partial, i.e. it won't break if the user<br>
presses Ctrl-S again before the text is ready:<br>
<br>
def handle_ctrl_s(self):<br>
filename = get_filename_from_user()<br>
self.page().toPlainText(partial(self.do_save, filename))<br>
<br>
def do_save(self, filename, data):<br>
with open(filename, 'w') as f:<br>
f.write(data)<br>
<br>
I think this is a general concept which is very important to grasp<br>
(and also took me a while) when programming with something like PyQt:<br>
<br>
Try to make everything happen async - the majority of the time should<br>
be spent in the Qt event loop (i.e. the app.exec_() call). Any code<br>
you write gets called from that event loop usually, and should finish<br>
"immediately". So here, you don't "wait" until the data is \
ready, you<br> tell Qt "call me back when the data is ready", return to the \
event<br> loop, and *then* save it when it *is* ready.<br>
<span class="HOEnZb"><font color="#888888"><br>
Florian<br>
<br>
--<br>
<a href="http://www.the-compiler.org" target="_blank">http://www.the-compiler.org</a> \
| <a href="mailto:me@the-compiler.org">me@the-compiler.org</a> \
(Mail/XMPP)<br>
GPG: 916E B0C8 FD55 A072 | <a href="http://the-compiler.org/pubkey.asc" \
target="_blank">http://the-compiler.org/pubkey.asc</a><br>
I love long mails! | <a href="http://email.is-not-s.ms/" \
target="_blank">http://email.is-not-s.ms/</a><br> \
</font></span><br>_______________________________________________<br> PyQt mailing \
list <a href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><br>
<a href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt" \
target="_blank">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br></blockquote></div><br></div>
[Attachment #6 (text/plain)]
_______________________________________________
PyQt mailing list PyQt@riverbankcomputing.com
http://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