[PySide] QLabel vs QWebView for simple html display

Frank Rueter | OHUfx frank at ohufx.com
Thu Nov 14 03:27:26 CET 2013


Ha, yes, indeed, thanks for pointing that out!
I'm initialising self.imageUrl in the constructor now as an empty string 
to avoid AttributeErrors, and have also incorporated your suggested 
tuple unpacking - much nicer that way.


Cheers,
frank

On 13/11/13 17:17, Sean Fisk wrote:
>
> Looks great! Thanks for sharing your approach. Keep in mind that if 
> the HTML fragment is untrusted and doesn’t have an |img| tag with a 
> |src| attribute, your app will crash on an |AttributeError|. Tuple 
> unpacking could make the for loop code a little more readable:
>
> |     def  handle_starttag(self, tag, attrs):
>          if  tag =='img':
>              for  attr_name, attr_valuein  attrs:
>                  if  attr_name =='src':
>                      self.imageUrl = attr_value|
>
> I like the use of built-in libraries. I used requests and lxml because 
> I am using them for a large project and already knew how to use them, 
> but they are definitely overkill for this.
>
>
>
> --
> Sean Fisk
>
>
> On Tue, Nov 12, 2013 at 7:28 PM, Frank Rueter | OHUfx <frank at ohufx.com 
> <mailto:frank at ohufx.com>> wrote:
>
>     just to follow up, here is what I have done so far (tried to stay
>     away from external libraries as I will have to distribute the app):
>
>     http://pastebin.com/VSjYBrSF
>
>     This seems to work for me but am happy to hear any comments on
>     this approach.
>
>
>     Cheers,
>     frank
>
>
>     On 12/11/13 21:13, Sean Fisk wrote:
>>
>>     Hi Frank,
>>
>>     I think it depends whether the HTML dictates the layout or you
>>     would like to dictate the layout:
>>
>>       * HTML dictates layout → Use the web view.
>>       * I want to dictate the layout → Parse the HTML to extract the
>>         URL, download the image manually, and put it into a QLabel.
>>
>>     Also remember that if you would like to distribute this and
>>     bundle PySide/Qt, using WebKit will add a significant size to
>>     your executable. If you truly need it, include it. However, for
>>     this, I don’t think it’s necessary.
>>
>>     From what you have said, it sounds like you would like to dictate
>>     the layout. The process isn’t exactly simple, but it’s doable in
>>     not too many lines. Off the top of my head:
>>
>>     |#!/usr/bin/env python
>>
>>     import  sys
>>
>>     from  PySideimport  QtGui
>>     import  requests
>>     import  lxml.html
>>
>>     def  main(argv):
>>          app = QtGui.QApplication(argv)
>>          widget = QtGui.QWidget()
>>          layout = QtGui.QVBoxLayout(widget)
>>          html ='''<p><span style="font-size: 17px;"><span style="color: #993300;"><img style="margin-right: 15px; vertical-align: top;" src="https://pypi.python.org/static/images/python-3.png" alt="announcements" /><cite>some text that is pulled from a website but which will be quite simple.</cite></span></span><img src="http://placekitten.com/200/100" /></p>'''   # NOPEP8
>>          doc = lxml.html.fragment_fromstring(html)
>>          for  img_src_attributein  doc.xpath('//img/@src'):
>>              response = requests.get(img_src_attribute)
>>              pixmap = QtGui.QPixmap()
>>              pixmap.loadFromData(response.content)
>>              label = QtGui.QLabel()
>>              label.setPixmap(pixmap)
>>              layout.addWidget(label)
>>          widget.show()
>>          widget.raise_()
>>          return  app.exec_()
>>
>>     if  __name__ =='__main__':
>>          raise  SystemExit(main(sys.argv))|
>>     ... which yields...
>>     Inline image 2
>>     I didn't address the issue of transparent backgrounds, etc.,
>>     because I'm not sure exactly what's going on there. But all three
>>     of the widgets on-screen are QLabels, so they should be able to
>>     do anything a QLabel can do.
>>
>>     Obviously, do some more error checking than I did if you are
>>     pulling this from a website with untrusted data. Let me know if
>>     you need help with that. Hope this helps!
>>
>>     Sincerely,
>>
>>
>>
>>     --
>>     Sean Fisk
>>
>>
>>     On Mon, Nov 11, 2013 at 11:26 PM, Frank Rueter | OHUfx
>>     <frank at ohufx.com <mailto:frank at ohufx.com>> wrote:
>>
>>         Hi all,
>>
>>         I have a QLabel in my app that displays some very simple html
>>         code
>>         pulled from a website.
>>
>>         So far I have been using it successfully as a sort of news
>>         feed, but now
>>         I need to display an image from a url as well, which QLabel
>>         can't do afaik.
>>
>>         So I had a go with QWebView but that just won't be have the
>>         same as
>>         QLabel when it comes to background colour (tried to set it to be
>>         transparent), sizes, size policies etc.
>>         I'm also getting some redraw problems when the QWebView is
>>         set to a
>>         transparent background.
>>
>>         Here is some test code:
>>         http://pastebin.com/cSizj9ET
>>
>>
>>         Is it the right approach to force QWebView to behave like
>>         QLabel for the
>>         sole reason of being able to display an image via html? Or is
>>         there a
>>         simpler way of doing this?
>>
>>
>>         Cheers,
>>         frank
>>         _______________________________________________
>>         PySide mailing list
>>         PySide at qt-project.org <mailto:PySide at qt-project.org>
>>         http://lists.qt-project.org/mailman/listinfo/pyside
>>
>>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131114/1a8dc570/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 62474 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131114/1a8dc570/attachment.png>


More information about the PySide mailing list