[PySide] QLabel vs QWebView for simple html display

Frank Rueter | OHUfx frank at ohufx.com
Wed Nov 13 01:28:08 CET 2013


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/20131113/ff98c5d7/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/20131113/ff98c5d7/attachment.png>


More information about the PySide mailing list