Pictures in pyqt5 gridlayout look stretched when window is maximized

92 Views Asked by At

The window shows a set of pictures. Once the window is maximized the app shows all pics in the window so it resizes every image looking ugly. I'd like to prevent the streching and keep the aspect ratio. Any clue?

This is the code of the proof of concept:

QApplication, QListWidget, QAbstractItemView, QPushButton, QWidget,
QTabWidget, QVBoxLayout, QHBoxLayout, QLabel, QPlainTextEdit,
QTextEdit, QCheckBox, QFileDialog, QMessageBox, QGridLayout from
PyQt5.QtCore import QProcess, QTimer, Qt from PyQt5.QtGui import QFont, QPixmap import os from QLabelClickable import QLabelClickable


class ExampleApp(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Example'
        self.left = 0
        self.top = 0
        self.width = 1000
        self.height = 800
        self.setMaximumWidth(1000)
        self.setMaximumHeight(800)
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.show()

        data = []

        for root, dirs, files in os.walk('img/'):
            for file in files:
                filepath = os.path.join(root, file)
                data.append(filepath)

        self.picture_window = PictureWindow(data=data)
        self.picture_window.show()

class PictureWindow(QWidget):
    def __init__(self, data):
        super().__init__()
        self.n_columns = 5
        self.data = data

        self.gridLayout = QGridLayout()
        for i, filename in enumerate(self.data):
            self.render_picture(filename=filename, pos=i)

        self.setLayout(self.gridLayout)

    def render_picture(self, filename, pos):
        image_widget = QLabelClickable(pos)
        image_widget.setGeometry(15, 15, 118, 130)
        image_widget.setToolTip(filename)
        image_widget.setCursor(Qt.PointingHandCursor)

        self.pixmapImagen = QPixmap(filename).scaled(375, 375, Qt.KeepAspectRatio, Qt.SmoothTransformation)
        image_widget.setPixmap(self.pixmapImagen)
        image_widget.setAlignment(Qt.AlignCenter)

        cell_layout = QVBoxLayout()
        cell_layout.setAlignment(Qt.AlignHCenter)
        cell_layout.addWidget(image_widget)
        self.gridLayout.addLayout(cell_layout, int(pos / self.n_columns), pos % self.n_columns)

from PyQt5.QtCore import Qt, pyqtSignal, QTimer
from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QMessageBox

class QLabelClickable(QLabel):
    clicked = pyqtSignal(str)

    def __init__(self, id, parent=None):
        super(QLabelClickable, self).__init__(parent)
        self.id = id

    def mousePressEvent(self, event):
        self.ultimo = "Click"

    def mouseReleaseEvent(self, event):
        if self.ultimo == "Click":
            QTimer.singleShot(QApplication.instance().doubleClickInterval(),
                              self.performSingleClickAction)
        else:
            self.clicked.emit(self.ultimo+"-"+str(self.id))

    def mouseDoubleClickEvent(self, event):
        self.ultimo = "Double Click"

    def performSingleClickAction(self):
        if self.ultimo == "Click":
            self.clicked.emit(self.ultimo+"-"+str(self.id))




if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = ExampleApp()
    sys.exit(app.exec_())

This is how it looks when window is normal: enter image description here

And looks like this when window is maximized: enter image description here

After a few days trying to figure out what's going on here I an't go further, could you please help me to spot anything wrong?

I expect the pics to keepe their aspect ratio to implement a ScrollArea later on.

0

There are 0 best solutions below