Reputation: 4568
I have the following code, that loads an image from disk and tries to scale it down to 30 x 30. Later i add the label to a gridlayout. Unfortunately the image is not being scaled down to the intended size so all my cells in the grid layout have diferent sizes.
pixmap = QtGui.QPixmap(filename)
pixmap.scaled(QtCore.QSize(30,30), QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
self.L.append(pixmap)
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
lbl.setScaledContents(True)
column=len(self.L)
self.ui.gridLayout.addWidget(lbl,0,column,Qt.AlignLeft | Qt.AlignTop)
Upvotes: 1
Views: 567
Reputation: 70324
Are you sure that pixmap.scaled
does an in-place transformation of the image? I would have expected it to return a new, scaled, image - assign that to a variable and use it instead.
According to this documentation:
Returns a scaled copy of the image. The returned image is scaled to the given height using the specified transformation mode. The width of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.
So, I guess you should be doing:
pixmap = QtGui.QPixmap(filename)
# FIXED:
scaled_pixmap = pixmap.scaled(QtCore.QSize(30,30), QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
self.L.append(scaled_pixmap) # FIXED
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
lbl.setScaledContents(True)
column=len(self.L)
self.ui.gridLayout.addWidget(lbl,0,column,Qt.AlignLeft | Qt.AlignTop)
Upvotes: 2