qlabel显示图片自适应
时间: 2023-06-04 15:03:43 浏览: 86
可以使用以下代码实现qlabel显示图片自适应:
```Python
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QLabel
class QPicLabel(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.setAlignment(Qt.AlignCenter)
def setPixmap(self, pixmap):
super().setPixmap(pixmap)
self.resize(pixmap.width(), pixmap.height())
def resizeEvent(self, event):
size = self.size()
pixmap = self.pixmap()
if pixmap and not pixmap.isNull():
scaledPixmap = pixmap.scaled(size, Qt.KeepAspectRatio,
Qt.SmoothTransformation)
self.setPixmap(scaledPixmap)
```
然后使用以下代码加载图片:
```Python
label = QPicLabel()
pixmap = QPixmap("image.png")
label.setPixmap(pixmap)
```