May 2019
Intermediate to advanced
542 pages
13h 37m
English
Qt offers a number of classes related to the use of images in an application, but, for simply displaying a picture in your GUI, the most appropriate is QPixmap. QPixmap is a display-optimized image class, which can load many common image formats including PNG, BMP, GIF, and JPEG.
To create one, we simply need to pass QPixmap a path to an image file:
logo = qtg.QPixmap('logo.png')
Once loaded, a QPixmap object can be displayed in a QLabel or QButton object, as follows:
heading.setPixmap(logo)
Note that labels can only display a string or a pixmap, but not both.
Being optimized for display, the QPixmap objects offer only minimal editing functionality; however, we can do simple transformations such as scaling:
if logo.width() ...
Read now
Unlock full access