Reputation: 5758
I am loading images in my GUI. I am using a QLabel for that pourpose like this:
self.myImage = QImage(os.path.join(folder,'samples0093.png'))
self.labelImage = QLabel()
self.labelImage.setAlignment(Qt.AlignCenter)
self.labelImage.setPixmap(QPixmap.fromImage(self.myImage))
self.gridLayout_8.addWidget(self.labelImage)
The problem is that the image does not adjust to the gridLayour, it is showed as big as it is. And it is much bigger than the space for the layout.
How can I force the image to adjust to a given with and height?
Upvotes: 1
Views: 1362
Reputation: 11
I was making an application and it landed here, here's an example that worked for me
qrcode_img = QLabel(mainWindow)
qrcode_img.setStyleSheet("background-color: rgb(255,255,255);")
qrcode_img.resize(300, 300)
qrcode_img.move(0,0)
qrcode_img.setPixmap(QPixmap("./src/imagem/qrcode.png").scaled(300,300))
Upvotes: 0
Reputation: 21268
I have made my comment the answer as it solved the initial issue.
Consider using scaledContents
property and set it to true
with QLabel::setScaledContents()
function for your label to adjust containing image size to the size of the label.
Upvotes: 1
Reputation: 1798
How can I force the image to adjust to a given with and height?
pixmap = QPixmap.fromImage(self.myImage)
QtGui.QImage(pixmap).scaled(theWith,thHeight, QtCore.Qt.KeepAspectRatio)
Upvotes: 0