简体   繁体   中英

QLabel load image, adjust size

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?

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.

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)

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM