'how to resize qlabel with image?
I have this snippet:
import sys
from PyQt5.QtWidgets import QLabel, QMainWindow, QApplication
from PyQt5.QtGui import QPixmap, QImage, QResizeEvent
import numpy as np
class VLabel(QLabel):
def __init__(self):
super(VLabel, self).__init__()
self.pixmap_width: int = 1
self.pixmapHeight: int = 1
def setPixmap(self, pm: QPixmap) -> None:
self.pixmap_width = pm.width()
self.pixmapHeight = pm.height()
self.updateMargins()
super(VLabel, self).setPixmap(pm)
def resizeEvent(self, a0: QResizeEvent) -> None:
self.updateMargins()
super(VLabel, self).resizeEvent(a0)
def updateMargins(self):
if self.pixmap() is None:
return
pixmapWidth = self.pixmap().width()
pixmapHeight = self.pixmap().height()
if pixmapWidth <= 0 or pixmapHeight <= 0:
return
w, h = self.width(), self.height()
if w <= 0 or h <= 0:
return
if w * pixmapHeight > h * pixmapWidth:
m = int((w - (pixmapWidth * h / pixmapHeight)) / 2)
self.setContentsMargins(m, 0, m, 0)
else:
m = int((h - (pixmapHeight * w / pixmapWidth)) / 2)
self.setContentsMargins(0, m, 0, m)
class ControlWindow(QMainWindow):
def __init__(self):
super(ControlWindow, self).__init__()
#frame = np.random.randint(0,255,(1080,1920,3), np.uint8)
frame = np.random.randint(0,255,(400,800,3), np.uint8)
img = QImage(frame, frame.shape[1], frame.shape[0],QImage.Format_RGB888)
pix = QPixmap.fromImage(img)
vl = VLabel()
vl.setPixmap(pix)
vl.setScaledContents(True)
self.setCentralWidget(vl)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = ControlWindow()
window.show()
window.raise_()
sys.exit(app.exec_())
Which shows an image in a QLabel. It works, and I can resize window bigger - and VLabel will scale automatically and fill up available space while keeping aspectration. Thats AWESOME!
HOWEVER...when i try to resize window to be smaller than initial image...i cannot :( For some reason the minimum size is the initial image size which is very unfortunate. How can i also enable resize of smaller windows so that when i drag-resize to something smaller than initial image size, then image will shrink accordingly (while still maintaining aspect ratio) ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
