QLabel load image, adjust size

Question:

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?

Asked By: codeKiller

||

Answers:

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)
Answered By: djangoliv

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.

Answered By: vahancho

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))
Answered By: PiedroCraft
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.