Hi! I’m trying out a few things for my application and am having some problems. This is my code (you can find full repo at Kazimierz Krauze / daikokuten · GitLab ):
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QColor, QPainter, QPixmap
from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel, QSizePolicy
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.title = "Image Viewer"
self.setWindowTitle(self.title)
self.root_pixmap = QPixmap('./daikokuten/test.jpg')
self.setMinimumSize(10, 10)
self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
def resizeEvent(self, event):
scaled = self.root_pixmap.scaled(
self.size(),
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation
)
print(self.size())
new_pixmap = QPixmap(scaled)
new_pixmap.fill(QColor("#ffffff"))
painter = QPainter(new_pixmap)
x = (new_pixmap.width() - scaled.width()) // 2
y = (new_pixmap.height() - scaled.height()) // 2
painter.drawPixmap(x, y, scaled)
painter.end()
label = QLabel(self)
label.setPixmap(new_pixmap)
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
When I run python ./daikokuten/main.py there is a window with an image however when I try to resize the window the image stays the same size.
Can you help me out?
Best,
Miro