Search code examples
pythonresizeqt-designerqlabelpyside6

Pyside6, How do I resize a QLabel without loosing the size aspect ratio?


My issue goes excactly as in those related posts:

Only difference is that I'm using Pyside instead of PyQt and that I've already found the answer but I did't want to create any confusion by posting it anywhere else besides here.


Solution

  • Thanks to this PyQt answer !!!

    I was able to find out how to do the same thing in PySide6 pretty flawlessly by following those simple example steps:

    • Open pyside6-designer\Qt-designer:
    1. Create a new window like this one: 2. Right-Click on label and go to "Promote to...":
    UI Promoted Widgets
    3. Select Form > View Python Code.. & save it as ui_main.py
    View Python Code

    click here if you have an issue at step 3.: "Unable to Launch Qt uic"

    • Create files :
    # custom_widgets.py
    
    from PySide6 import QtCore, QtGui, QtWidgets
    
    
    class ScaledLabel(QtWidgets.QLabel):
        def __init__(self, *args, **kwargs):
            QtWidgets.QLabel.__init__(self)
            self._pixmap = self.pixmap()
            self._resised= False
        
        def resizeEvent(self, event):
            self.setPixmap(self._pixmap)     
    
        def setPixmap(self, pixmap): #overiding setPixmap
            if not pixmap:return 
            self._pixmap = pixmap
            return QtWidgets.QLabel.setPixmap(self,self._pixmap.scaled(
                    self.frameSize(),
                    QtCore.Qt.KeepAspectRatio))
    
    # main.py
    
    from   PySide6.QtWidgets import *
    from   PySide6           import QtCore
    from   PySide6.QtGui     import QPixmap
    from   PySide6.QtCore    import QPropertyAnimation
    from   ui_main           import Ui_MainWindow
    import sys, os
    
    
    CWD = os.path.dirname(__file__) + os.sep
    class MainWindow(QMainWindow): 
        def __init__(self):
            QMainWindow.__init__(self)
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            self.ui.label.setPixmap(QPixmap(CWD + 'lenna.jpg'))
            self.ui.pushButton.clicked.connect(lambda: self.resizeMainWindow(1200,500))
            self.show()
    
        def resizeMainWindow(self, width, height):
            self.animation = QPropertyAnimation(self, b"size")
            self.animation.setLoopCount(3)
            self.animation.setDuration(3000)
            self.animation.setKeyValueAt(0   ,QtCore.QSize(self.width()  ,self.height()  ))
            self.animation.setKeyValueAt(0.25,QtCore.QSize(self.width()/2,self.height()/2))
            self.animation.setKeyValueAt(0.5 ,QtCore.QSize(self.width()/2,self.height()  ))
            self.animation.setKeyValueAt(0.75,QtCore.QSize(self.width()  ,self.height()/2))
            self.animation.setKeyValueAt(1   ,QtCore.QSize(self.width()  ,self.height()  ))
            self.animation.setEasingCurve(QtCore.QEasingCurve.InOutSine)
            self.animation.start()
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MainWindow()
        sys.exit(app.exec_())
        
        
        
    """
     * [QPixmap only works with absolute Path](https://stackoverflow.com/a/26323327/11465149)
     * [Relative paths in Python](https://stackoverflow.com/a/918178/11465149)
     * [resizeMainWindow()](https://github.com/Wanderson-Magalhaes/QPropertyAnimation_PySide2_PyQt5_Widgets_Animation/blob/master/main.py#L60)
    """
    

    if everything went fine so far, you should also have a kind of similar ui_main.py file in your working directory like this one.

    • ✔️ And you should have a working example like the one demonstrated here left:
    With ScaledLabel With             QLabel
    gif1 gif1