使用更改窗口大小调整包含图像的多个标签

问题描述 投票:1回答:1

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

目前,在我的情况下使用resizeEvent()不会根据resize函数缩小图像。它已经通过我的窗体窗口的显示触发,从而使pushButton无用。最重要的是,由此产生的执行速度非常慢。我的图像尺寸为2058x1536,透明显示。

我的qt设计师代码在这里给出: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()

有没有解决方案来更快地运行此代码?例如,我想在鼠标点击窗口边缘时使所有标签变为空白,然后在释放鼠标按钮后再次出现图像。这似乎并不那么整洁。此外,我不确定使用paintEvent是否可以减少我的滞后。感谢您的建议和意见。

python python-3.x pyqt pyqt5 qlabel
1个回答
0
投票

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()
© www.soinside.com 2019 - 2024. All rights reserved.