[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">&lt;<a href="mailto:me@the-compiler.org" \
target="_blank">me@the-compiler.org</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">* David Cortesi &lt;<a \
href="mailto:davecortesi@gmail.com">davecortesi@gmail.com</a>&gt; [2015-01-05 \
19:59:13 -0800]:<br> <div><div class="h5">&gt; On Mon, Jan 5, 2015 at 3:26 PM, \
Andreas Pakulat &lt;<a href="mailto:apaku@gmx.de">apaku@gmx.de</a>&gt; wrote:<br> \
&gt;<br> &gt; &gt;<br>
&gt; &gt; I&#39;m a bit at a loss what to do following a call to such a method. What \
is<br> &gt; &gt;&gt; the best way to &quot;idle&quot; until the callback comes? n.b. \
I don&#39;t see any<br> &gt; &gt;&gt; WebEngine specific guidance at the PyQt5 doc \
[2].<br> &gt; &gt;&gt;<br>
&gt; &gt;<br>
&gt; &gt; I&#39;d say just as all other callbacks in Qt (i.e. signal/slots), \
return<br> &gt; &gt; control to the qt event loop.<br>
&gt; &gt;<br>
&gt;<br>
&gt; Could you clarify a bit? The doc [1] for QWebEnginePage.toPlainText() will<br>
&gt; at some unknown future time call my callback passing the text of the page.<br>
&gt; But I would make that call in some code that needs that text -- to write it<br>
&gt; to a file, say. So in a custom class based on QWebEngineView, I capture the<br>
&gt; ^S key, perhaps, for Save. I put a findSaveDialog and get a path string and<br>
&gt; open a text stream for output. Then...<br>
&gt;<br>
&gt;        self.page().toPlainText( lambda s : self.save_plain_text = s )<br>
&gt;        # Here --   twiddle my thumbs until I can...<br>
&gt;        output_stream &lt;&lt; self.save_plain_text<br>
&gt;<br>
&gt; Is this the kind of thing you have in mind?<br>
&gt;<br>
&gt;        self.save_plain_text = None<br>
&gt;        self.page().toPlainText( lambda s : self.save_plain_text = s )<br>
&gt;        while self.save_plain_text is None : QCoreApplication.processEvents()<br>
&gt;        output_stream &lt;&lt; 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, &#39;w&#39;) as f:<br>
                  f.write(data)<br>
<br>
Or this using functools.partial, i.e. it won&#39;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, &#39;w&#39;) 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>
&quot;immediately&quot;. So here, you don&#39;t &quot;wait&quot; until the data is \
ready, you<br> tell Qt &quot;call me back when the data is ready&quot;, 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