Pyqt6 pixmap - image does not resize upon window resizing

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

I don’t understand what the target is. I am working on a similiar problem. See my simple suggestion. There is also scaledToHeight available. Have a look in the docu for further info.

import sys
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QColor, QPainter, QPixmap
from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel, QSizePolicy, QVBoxLayout

'''
Not all the imports are applied.
Substitute the image filename
'''


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = "Image Viewer"
        self.setWindowTitle(self.title)

        self.root_pixmap = QPixmap('rotation0.png')
        self.setMinimumSize(100, 100)

#        self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
        
        layout = QVBoxLayout()
        self.label = QLabel(self)

        self.label.setGeometry(0, 0, 400, 400)
        layout.addWidget(self.label)

        self.resize(self.label.size().width(), self.label.size().height())
        

    def resizeEvent(self, event):

        newsize = self.size()
#        print(newsize)
        self.label.resize(newsize.width(), newsize.height())
        newpix = self.root_pixmap.scaledToWidth(newsize.width())
        self.label.setPixmap(newpix)

app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())

Thank you! It helped.