调整包含具有更改窗口大小的图像的多个标签的大小
Posted
技术标签:
【中文标题】调整包含具有更改窗口大小的图像的多个标签的大小【英文标题】:Resize multiple labels containing images with changing window size 【发布时间】:2019-02-19 13:32:50 【问题描述】:我有一个窗口,有六个对称放置的标签,所有标签都显示图像(使用 qt-designer 在布局的帮助下设计)。我想根据不断变化的窗口大小调整这些图像的大小。我在以前的问题中找到了一些帮助,例如:PyQt: Detect resizing in Widget-window resized signal
目前,在我的情况下使用 resizeEvent() 不会根据 resize 函数缩小图像。它已经被我的表单窗口的显示触发,从而使 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()
是否有任何解决方案可以更快地运行此代码?例如,我想在我的窗口边缘单击鼠标时让所有标签变为空白,然后在释放鼠标按钮后重新出现图像。这似乎不那么整洁。另外,我不确定使用paintEvent 是否可以减少我的延迟。感谢您的建议和cmets。
【问题讨论】:
【参考方案1】: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()
【讨论】:
label.setScaledContents(True) 是解决方案。此外,将这些标签放入列表使我的代码更短。以上是关于调整包含具有更改窗口大小的图像的多个标签的大小的主要内容,如果未能解决你的问题,请参考以下文章