通过更改 window 大小调整包含图像的多个标签的大小

Resize multiple labels containing images with changing window size

我有一个 window 有六个对称放置的标签,所有标签都显示图像(在布局的帮助下使用 qt-designer 设计)。我想根据不断变化的 window 大小调整这些图像的大小。我在之前的问题中找到了一些帮助,例如:

目前,在我的案例中使用resizeEvent() 不会根据resize 函数缩小图像。它已经随着我的表单 window 的显示而触发,从而使 pushButton 无用。最重要的是,由此产生的执行速度非常慢。我的图像尺寸为 2058x1536 并且透明显示。

我的qt-designer代码在这里:https://pastebin.com/TzM6qiKZ

import Ui_ImageCrop_Test
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QPixmap, QImage, QPainter, QColor
from PyQt5.QtCore import Qt

class ImageCrop(Ui_ImageCrop_Test.Ui_MainWindow, QMainWindow):
    def __init__(self, parent=None):
        super(ImageCrop, self).__init__()
        self.setupUi(self)
        self.transparency = 220

        with open("Img_files.txt") as file:
            self.img_files = file.read().splitlines()
        self.length = len(self.img_files)

        self.pushButton_1.clicked.connect(self.click1)
        self.label_1.resizeEvent = self.click1

    def click1(self, event):
        for i in range(6):
            image = QImage(self.img_files[i])
            image = image.convertToFormat(QImage.Format_ARGB8565_Premultiplied)

            p = QPainter(image)
            p.setCompositionMode(QPainter.CompositionMode_DestinationIn)
            p.fillRect(image.rect(), QColor(0, 0, 0, self.transparency))
            p.end()

            pixmap = QPixmap(image)
            w = int(self.label_1.width() - 4.0)
            h = int(self.label_1.height() - 4.0)
            smaller_pixmap = pixmap.scaled(w, h, Qt.IgnoreAspectRatio, Qt.FastTransformation)

            if i == 0:
                self.label_1.setPixmap(smaller_pixmap)

            if i == 1:
                self.label_2.setPixmap(smaller_pixmap)

            if i == 2:
                self.label_3.setPixmap(smaller_pixmap)

            if i == 3:
                self.label_4.setPixmap(smaller_pixmap)

            if i == 4:
                self.label_5.setPixmap(smaller_pixmap)

            if i == 5:
                self.label_6.setPixmap(smaller_pixmap)


def main():
    app = QApplication(sys.argv)
    form1 = ImageCrop()
    form1.show()
    app.exec_()


if __name__ == '__main__': main()

有什么解决方案可以使运行这段代码更快吗?例如,我想在我的 window 边缘单击鼠标时让我所有的标签变成空白,然后在释放鼠标按钮后图像重新出现。这看起来不太整洁。另外,我不确定使用 paintEvent 是否可以减少延迟。感谢您的建议和意见。

QLabel 具有允许图像自动缩放的 scaledContents 属性:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import Ui_ImageCrop_Test

class ImageCrop(QtWidgets.QMainWindow, Ui_ImageCrop_Test.Ui_MainWindow):
    def __init__(self, parent=None):
        super(ImageCrop, self).__init__()
        self.setupUi(self)
        self.pushButton_1.clicked.connect(self.click1)
        self.transparency = 220
        with open("Img_files.txt") as file:
            self.img_files = file.read().splitlines()

    @QtCore.pyqtSlot()
    def click1(self):
        labels = [self.label_1, self.label_2, self.label_3, 
            self.label_4, self.label_5, self.label_6]
        for label, filename in zip(labels, self.img_files):
            image = QtGui.QImage(filename)
            image = image.convertToFormat(QtGui.QImage.Format_ARGB8565_Premultiplied)

            p = QtGui.QPainter(image)
            p.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationIn)
            p.fillRect(image.rect(), QtGui.QColor(0, 0, 0, self.transparency))
            p.end()
            pixmap = QtGui.QPixmap(image)
            w = int(label.width() - 4.0)
            h = int(label.height() - 4.0)
            smaller_pixmap = pixmap.scaled(w, h, QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.FastTransformation)
            label.setPixmap(smaller_pixmap)
            label.setScaledContents(True)

def main():
    app = QtWidgets.QApplication(sys.argv)
    form1 = ImageCrop()
    form1.show()
    app.exec_()

if __name__ == '__main__': main()