I want to display an image in pyqt so,i used a label and the pixmap option,and the scaledContents but the image is distorted.Should I use another widget or do something else? Thanks.
This is the code:
from PyQt4 import QtCore, QtGui
self.label.setPixmap(QtGui.QPixmap(_fromUtf8('image.jpg')))
self.label.setScaledContents(True)
I use QtDesigner.
I tried this: self.label.pixmap().scaled(QtCore.QSize(self.label.size()), QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
but the images don't get resized to fit the label.
A QPixmap can be used to show an image in a PyQT window. QPixmap() can load an image, as parameter it has the filename. To show the image, add the QPixmap to a QLabel. QPixmap supports all the major image formats: BMP,GIF,JPG,JPEG,PNG,PBM,PGM,PPM,XBM and XPM.
From the property editor dropdown select "Choose File…" and select an image file to insert. As you can see, the image is inserted, but the image is kept at its original size, cropped to the boundaries of the QLabel box. You need to resize the QLabel to be able to see the entire image.
Widgets are the basic building blocks for graphical user interface (GUI) applications built with Qt. Each GUI component (e.g. buttons, labels, text editors) is a widget that is placed somewhere within a user interface window, or is displayed as an independent window.
Your PyQt6 license must be compatible with your Qt license. If you use the GPL license, then your code must also use a GPL-compatible license.
Use the scaled(const QSize, Qt::AspectRatioMode, Qt::TransformationMode)
method of the pixmap, it has an option Qt::KeepAspectRatio
that does not deform the image. The default is to ignore the aspect ratio
Also, note that the scaled
method returns the scaled pixmap
, so it must be used this way:
myPixmap = QtGui.QPixmap(_fromUtf8('image.jpg'))
myScaledPixmap = myPixmap.scaled(self.label.size(), Qt.KeepAspectRatio)
self.label.setPixmap(myScaledPixmap)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With