[PySide] QLabel crops pixmap

Sean Fisk sean at seanfisk.com
Fri Mar 18 22:28:33 CET 2016


Hi Frank,

I’m mostly seeing similar results. Here is my test program:

#!/usr/bin/env python
import sysimport argparse
from PySide import QtGui

arg_parser = argparse.ArgumentParser(
    description='Display an SVG via QPixmap.')
arg_parser.add_argument('svg_path', help='SVG file to display')
args = arg_parser.parse_args()

app = QtGui.QApplication([])

pixmap = QtGui.QPixmap(args.svg_path)
label = QtGui.QLabel()
label.setPixmap(pixmap)

label.show()
label.raise_()

sys.exit(app.exec_())

OS XWindows
[image: Inline image 3] [image: Inline image 2]

The SVG does not display at all under Windows. However, checking the
documentation for QPixmap
<http://doc.qt.io/qt-4.8/qpixmap.html#reading-and-writing-image-files>
suggests checking QImageReader::supportedImageFormats() for a full list of
formats. Doing this yields:

#!/usr/bin/env python
from __future__ import print_function
from PySide import QtGui

print(*QtGui.QImageReader.supportedImageFormats(), sep='\n')

OS XWindows

bmp
gif
ico
jpeg
jpg
mng
pbm
pgm
png
ppm
svg
svgz
tga
tif
tiff
xbm
xpm

bmp
gif
ico
jpeg
jpg
mng
pbm
pgm
png
ppm
tga
tif
tiff
xbm
xpm

SVG and SVGZ are not on the list for Windows. This indicates to me that Qt
will either not display those image types or display them incorrectly. I
would personally suggest using QSvgWidget, which utilizes QSvgRenderer but
is a lot easier to use. This works for me on both operating systems:

#!/usr/bin/env python
import sysimport argparse
from PySide import QtGui, QtSvg

arg_parser = argparse.ArgumentParser(
    description='Display an SVG via QSvgWidget.')
arg_parser.add_argument('svg_path', help='SVG file to display')
args = arg_parser.parse_args()

app = QtGui.QApplication([])

widget = QtSvg.QSvgWidget(args.svg_path)

widget.show()
widget.raise_()

sys.exit(app.exec_())

OS XWindows
[image: Inline image 4] [image: Inline image 5]

If you’re like me, it bothers you that Qt does not respect the aspect ratio
of the SVG on resize. For that, I created AspectRatioSvgWidget:

from __future__ import division
class AspectRatioSvgWidget(QtSvg.QSvgWidget):
    def paintEvent(self, paint_event):
        painter = QtGui.QPainter(self)
        default_width, default_height = self.renderer().defaultSize().toTuple()
        widget_width, widget_height = self.size().toTuple()
        ratio_x = widget_width / default_width
        ratio_y = widget_height / default_height
        if ratio_x < ratio_y:
            new_width = widget_width
            new_height = widget_width * default_height / default_width
            new_left = 0
            new_top = (widget_height - new_height) / 2
        else:
            new_width = widget_height * default_width / default_height
            new_height = widget_height
            new_left = (widget_width - new_width) / 2
            new_top = 0
        self.renderer().render(
            painter,
            QtCore.QRectF(new_left, new_top, new_width, new_height))

I’ve included a copy of all the sources including the test SVG I used in an
attachment. Hope this helps, Frank! Let me know :)

~ Sean

Versions of everything:

OS XWindows

Platform: Darwin-14.5.0-x86_64-i386-64bit
Darwin version info: (‘10.10.5’, (‘’, ‘’, ‘’), ‘x86_64’)
Python compiler: GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)
Python version: CPython 2.7.11
Python interpreter architecture: bits=’64bit’ linkage=’’
PySide version: 1.2.4
PySide version tuple: (1, 2, 4, ‘final’, 0)
Compiled with Qt: 4.8.7
Running with Qt: 4.8.7

Platform: Windows-8.1-6.3.9600
Windows version info: (‘8.1’, ‘6.3.9600’, ‘’, u’Multiprocessor Free’)
Python compiler: MSC v.1500 64 bit (AMD64)
Python version: CPython 2.7.11
Python interpreter architecture: bits=’64bit’ linkage=’WindowsPE’
PySide version: 1.2.4
PySide version tuple: (1, 2, 4, ‘final’, 0)
Compiled with Qt: 4.8.7
Running with Qt: 4.8.7

#!/usr/bin/env python
from __future__ import print_functionimport platformimport argparse
import PySide

arg_parser = argparse.ArgumentParser(
    description='Print information about the platform, Python, and PySide.')
arg_parser.parse_args()

platform_funcs = dict(
    Windows=platform.win32_ver,
    Darwin=platform.mac_ver,
    Linux=platform.linux_distribution,
)
system = platform.system()
for key, val in [
    ('Platform', platform.platform()),
    ('{} version info'.format(system), platform_funcs[system]()),
    ('Python compiler', platform.python_compiler()),
    ('Python version', '{} {}'.format(
        platform.python_implementation(), platform.python_version())),
    ('Python interpreter architecture',
     'bits={!r} linkage={!r}'.format(*platform.architecture())),
    ('PySide version', PySide.__version__),
    ('PySide version tuple', PySide.__version_info__),
    ('Compiled with Qt', PySide.QtCore.__version__),
    ('Running with Qt', PySide.QtCore.qVersion()),
]:
    print('{}: {}'.format(key, val))

​


--
Sean Fisk

On Mon, Mar 14, 2016 at 3:51 PM, Frank Rueter | OHUfx <frank at ohufx.com>
wrote:

> Nobody?
> Guess I will use pngs then until I can figure this out.
>
>
> On 14/03/16 3:36 pm, Frank Rueter | OHUfx wrote:
>
> Hi all,
>
> so I have realised that its not the QLabel but the QPixmap in combination
> to a vector graphic (svg).
> Below is my test code which works fine on osx but crops the image on the
> right hand side when run under windows.
> The svg was saved with a 200 pixel output resolution in the header, and
> when I check the QPixmap's width it does return 200, still it crops the
> image.
> Do I actually have to start using Qt.QSvgRender for something simple like
> this?
>
> Cheers,
> frank
>
>
> from PySide import QtGui
> def pixmapTest(imgPath):
>     l = QtGui.QLabel()
>     l.setPixmap(QtGui.QPixmap(imgPath))
>     return l
>
> if __name__ == '__main__':
>     import sys
>     app = QtGui.QApplication([])
>     if sys.platform == 'win32':
>         imgPath = 'z:/path/to/svg/image.svg'
>     else:
>         imgPath = '/server/path/path/to/svg/image.svg'
>     l = pixmapTest(imgPath)
>     l.show()
>     sys.exit(app.exec_())
>
> On 11/03/16 7:46 pm, Frank Rueter | OHUfx wrote:
>
> Hi,
>
> I have been using something like the below code on osx without trouble
> (simple QLabel with setPixmap to show an svg file from my resource module).
>
> When running the same code on windows, the label crops the image on the
> right, and I cannot figure out how to make it behave the same as under osx
> (adjust to the pixmap's size). Even brudte forcing it's width to somethign
> much larger than the pixmap will still result in a cropped display.
>
> Does anybody know what might be going on?
>
> Cheers, frank
>
> import common
>
> from PySide.QtGui import * # for testing only
>
> from PySide.QtCore import * # for testing only
>
> class TestImageLabel(QLabel):
>
> def __init__(self, parent=None):
>
> super(TestImageLabel, self).__init__(parent)
>
> self.pm = common.IconCache.getPixmap('nuBridge_logo')
>
> self.setPixmap(self.pm)
>
> def showEvent(self, e):
>
> super(TestImageLabel, self).showEvent(e)
>
> print self.pm.width()
>
> print self.width()
>
> w = TestImageLabel()
>
> w.show()
>
> --
> [image: ohufxLogo 50x50] <http://www.ohufx.com> *vfx compositing
> <http://ohufx.com/index.php/vfx-compositing> | workflow customisation and
> consulting <http://ohufx.com/index.php/vfx-customising> *
>
>
> _______________________________________________
> PySide mailing listPySide at qt-project.orghttp://lists.qt-project.org/mailman/listinfo/pyside
>
>
> --
> [image: ohufxLogo 50x50] <http://www.ohufx.com> *vfx compositing
> <http://ohufx.com/index.php/vfx-compositing> | workflow customisation and
> consulting <http://ohufx.com/index.php/vfx-customising> *
>
>
> _______________________________________________
> PySide mailing listPySide at qt-project.orghttp://lists.qt-project.org/mailman/listinfo/pyside
>
>
> --
> [image: ohufxLogo 50x50] <http://www.ohufx.com> *vfx compositing
> <http://ohufx.com/index.php/vfx-compositing> | workflow customisation and
> consulting <http://ohufx.com/index.php/vfx-customising> *
>
> _______________________________________________
> PySide mailing list
> 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/20160318/7c7017ac/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 2666 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160318/7c7017ac/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pixmap-osx.png
Type: image/png
Size: 27856 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160318/7c7017ac/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: svgwidget-win.png
Type: image/png
Size: 7528 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160318/7c7017ac/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: svgwidget-osx.png
Type: image/png
Size: 28639 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160318/7c7017ac/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pixmap-win.png
Type: image/png
Size: 959 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160318/7c7017ac/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ohufxLogo_50x50.png
Type: image/png
Size: 2666 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160318/7c7017ac/attachment-0005.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 2666 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160318/7c7017ac/attachment-0006.png>


More information about the PySide mailing list