[PySide] QLabel crops pixmap

Tibold Kandrai kandraitibold at gmail.com
Sat Mar 19 00:19:19 CET 2016


Hey guys!

 

I use QIcon for SVG rendering, had no issues with it on either platoforms. It does require the icon svg plugins to be loaded.

 

from PySide improt QtGui

 

app = QtGui.Qapplication([])

icon = QtGui.Qicon(’path/to/svg.svg’)

pixmap = icon.pixmap(100, 200) # Pixmap size may be smaller, refer to the docs 

label = QtGui.QLabel()

label.setPixmap(pixmap)

label.show()

 

Tibold Kandrai

Software Architect & Engineer

 

From: PySide [mailto:pyside-bounces+kandraitibold=gmail.com at qt-project.org] On Behalf Of Frank Rueter | OHUfx
Sent: Friday, 18 March, 2016 23:24
To: Sean Fisk <sean at seanfisk.com>
Cc: PySide <pyside at qt-project.org>
Subject: Re: [PySide] QLabel crops pixmap

 

Great, thanks a lot Sean!
I have to run now but will take a closer look on the weekend.

Cheers,
frank

On 19/03/16 10:30 am, Sean Fisk wrote:

Oops, forgot the attachment! Here you go.

On Fri, Mar 18, 2016 at 5:28 PM Sean Fisk <sean at seanfisk.com <mailto:sean at seanfisk.com> > wrote:

Hi Frank,

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

#!/usr/bin/env python
 
import sys
import 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 X

Windows






The SVG does not display at all under Windows. However, checking the documentation for  <http://doc.qt.io/qt-4.8/qpixmap.html#reading-and-writing-image-files> QPixmap 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 X

Windows


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 sys
import 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 X

Windows






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 X

Windows


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_function
import platform
import 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 <mailto: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 <http://self.pm>  = common.IconCache.getPixmap('nuBridge_logo')

self.setPixmap(self.pm <http://self.pm> )

def showEvent(self, e):

super(TestImageLabel, self).showEvent(e)

print self.pm.width()

print self.width()

w = TestImageLabel()

w.show()

-- 


 <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 <mailto:PySide at qt-project.org> 
http://lists.qt-project.org/mailman/listinfo/pyside

 

-- 


 <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 <mailto:PySide at qt-project.org> 
http://lists.qt-project.org/mailman/listinfo/pyside

 

-- 


 <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 <mailto:PySide at qt-project.org> 
http://lists.qt-project.org/mailman/listinfo/pyside

 

 

-- 


 <http://www.ohufx.com> 

vfx compositing <http://ohufx.com/index.php/vfx-compositing>  | workflow customisation and consulting <http://ohufx.com/index.php/vfx-customising>  

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160319/ce3cc3e8/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 27856 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160319/ce3cc3e8/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 959 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160319/ce3cc3e8/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 28639 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160319/ce3cc3e8/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.png
Type: image/png
Size: 7528 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160319/ce3cc3e8/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image005.png
Type: image/png
Size: 2666 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160319/ce3cc3e8/attachment-0004.png>


More information about the PySide mailing list