Search code examples
qtpyqt4qpixmapqgridlayout

PyQt4 - make child widgets scale down to fit inside parent and layout


For example, I have a gridlayout which contains some QPixmaps. However, if the images in the QPixmaps are bigger than the cells of the gridlayout, they overflow and end up overlapping each other. How can I make them automatically scale down so that they fit in the layout? I have searched everywhere for this answer and cannot figure it out. Please help, thanks!


Solution

  • Use scaled() method from QPixmap class to to sacle down the pixmaps to a size that suits your situation. For example:

    pix = QPixmap("path/to/image.png")
    newPix = pix.scaled(w, h, Qt.KeepAspectRatio)
    

    This snippet will scale the pix to wxh.