[PySide] QLabel vs QWebView for simple html display
sean at seanfisk.com
Wed Nov 13 05:17:45 CET 2013
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
srcattribute, your app will crash on an
AttributeError. Tuple unpacking could make the for loop code a little more
def handle_starttag(self, tag, attrs):
if tag == 'img':
for attr_name, attr_value in 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.
On Tue, Nov 12, 2013 at 7:28 PM, Frank Rueter | OHUfx <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):
> This seems to work for me but am happy to hear any comments on this
> 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
> 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 PySide import QtGuiimport requestsimport 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_attribute in doc.xpath('//img/@src'):
> response = requests.get(img_src_attribute)
> pixmap = QtGui.QPixmap()
> label = QtGui.QLabel()
> return app.exec_()
> if __name__ == '__main__':
> raise SystemExit(main(sys.argv))
> ... which yields...
> [image: 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
> 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!
> Sean Fisk
> On Mon, Nov 11, 2013 at 11:26 PM, Frank Rueter | OHUfx <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
>> 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:
>> 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?
>> PySide mailing list
>> PySide at qt-project.org
-------------- next part --------------
An HTML attachment was scrubbed...
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 62474 bytes
Desc: not available
More information about the PySide