python - 在保持图像纵横比的同时,在水平布局内调整像素图大小行为的标签不一致

标签 python pyqt pyqt5 qlabel qpixmap

我正在尝试使用 PyQt5 创建图像编辑器。

我对 QLabel 进行了子类化,以创建一个通过 setPixmap 显示图像的标签。该类确保无论何时调整标签大小,图像都保持纵横比。

我将其中 2 个标签放入水平布局中,并将它们的像素图设置为 2 个图像。

一切都很好,除非以某种方式调整窗口大小。

当我开始增加窗口宽度时,一切都很好。但是当我开始缩小它时,第一个标签开始缩小,而第二个标签保持不变。第一个标签不断缩小,直到尺寸无法再缩小,从而迫使第二个标签缩小。

这不是我想要的。我希望在调整窗口大小时两个标签保持相同的大小。我怎样才能做到这一点?

这是重现我的问题的代码的最小部分:

import sys

from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QPixmap

class ImageLabel(QtWidgets.QLabel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setMinimumSize(1,1)
        self.setScaledContents(False)

        self.pixmap = QPixmap(1, 1)

    def set_image(self, image_path):
        pixmap = QPixmap(image_path)
        self.set_pixmap(pixmap)

    def set_pixmap(self, pixmap):
        self.pixmap = pixmap
        self.setPixmap(pixmap)

    def resizeEvent(self, event):
        self.setPixmap(self.scaled_pixmap())

    def scaled_pixmap(self):
        return self.pixmap.scaled(
            self.size(),
            Qt.KeepAspectRatio,
            Qt.SmoothTransformation
        )

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi()
        self.start()

    def setupUi(self):
        self.setWindowTitle('Image Editor')

        self.centralWidget = QtWidgets.QWidget()

        self.hlayout = QtWidgets.QHBoxLayout()

        self.image_1 = ImageLabel()
        self.hlayout.addWidget(self.image_1)

        self.image_2 = ImageLabel()
        self.hlayout.addWidget(self.image_2)

        self.centralWidget.setLayout(self.hlayout)
        self.setCentralWidget(self.centralWidget)

        self.resize(800, 600)

    def start(self):
        self.image_1.set_image(
            r"orig.jpg"
        )

        self.image_2.set_image(
            r"edit.jpg"
        )

app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()

mainWindow.show()
exitCode = app.exec_()

sys.exit(exitCode)

最佳答案

最简单的方法是使用带有 QGraphicsPixmapItem 的 QGraphicsView,而不是使用 QLabel:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class ImageLabel(QtWidgets.QGraphicsView):
    def __init__(self, *args, **kwargs):
        super(ImageLabel, self).__init__(*args, **kwargs)
        self.setScene(QtWidgets.QGraphicsScene())
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) 
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

    def setImage(self, filename):
        self.setPixmap(QtGui.QPixmap(filename))

    def setPixmap(self, pixmap):
        item = QtWidgets.QGraphicsPixmapItem(pixmap)
        item.setTransformationMode(QtCore.Qt.SmoothTransformation)
        self.scene().addItem(item)

    def resizeEvent(self, event):
        r = self.scene().itemsBoundingRect()
        self.fitInView(r, QtCore.Qt.KeepAspectRatio)
        super(ImageLabel, self).resizeEvent(event)


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi()
        self.start()

    def setupUi(self):
        self.setWindowTitle('Image Editor')

        self.image_1 = ImageLabel()
        self.image_2 = ImageLabel()

        self.centralWidget = QtWidgets.QWidget()
        self.setCentralWidget(self.centralWidget)

        hlayout = QtWidgets.QHBoxLayout(self.centralWidget)
        hlayout.addWidget(self.image_1)
        hlayout.addWidget(self.image_2)

        self.resize(800, 600)

    def start(self):
        self.image_1.setImage(
            r"orig.jpg"
        )

        self.image_2.setImage(
            r"edit.jpg"
        )

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = MainWindow()

    mainWindow.show()
    exitCode = app.exec_()

    sys.exit(exitCode)

关于python - 在保持图像纵横比的同时,在水平布局内调整像素图大小行为的标签不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54950094/

相关文章:

python - 散列一系列值

python - PyQt 中基于鼠标位置的多个上下文菜单

python - PIL 图像到 QPixmap 的转换问题

python - 在 Python3 中将小部件应用程序作为线程启动?

python - Qtablewidget去除黑色空间PyQt5

Python编程: Classes

python - 使用 Dryscrape 登录 Facebook

qt - PyQt 事件处理和 QErrorMessage : detect the source of the click

python - 打开一个新窗口

python - 从配置文件自定义 python 结构化 (json) 日志记录中的键值