gpt4 book ai didi

python - QFileDialog 为所有支持的图像格式创建过滤器

转载 作者:行者123 更新时间:2023-12-01 08:19:31 24 4
gpt4 key购买 nike

我想打开一个包含所有支持的图像格式的 QFileDialog.getOpenFileName(我可以用来实例化 QIcon 的所有文件类型)

我已经知道我可以使用 QImageReader.supportedImageFormats() 获取所有支持的图像格式。

令我困惑的是,QImageReader.supportedImageFormats() 返回一个 QBytesArray 列表,我不知道如何将其简单地转换为 列表>str

class ProfileImageButton(qt.QToolButton):
def __init__(self, parent=None):
super().__init__(parent)
self.setIconSize(qt.QSize(100, 100))
self.clicked.connect(self._onClick)
self._icon_path = None

def _onClick(self, checked):
supportedFormats = qt.QImageReader.supportedImageFormats()
print([str(fo) for fo in supportedFormats])
# this prints: ["b'bmp'", "b'cur'", "b'gif'", "b'icns'", "b'ico'", "b'jpeg'",

fname, filter_ = qt.QFileDialog.getOpenFileName(
parent=self,
caption="Load a profile picture",)
# filter=???????????) # <--- TODO

if fname:
self.setIcon(qt.QIcon(fname))
self.setIconSize(qt.QSize(100, 100))
self._icon_path = fname

def iconPath(self):
return self._icon_path

最佳答案

您必须使用 data() 方法将 QByteArray 转换为 bytes,然后将字节转换为 string > 使用decode()。然后仅进行串联以获得所需的格式。

text_filter = "Images ({})".format(" ".join(["*.{}".format(fo.data().decode()) for fo in supportedFormats]))

fname, _ = qt.QFileDialog.getOpenFileName(
parent=self,
caption="Load a profile picture",
filter=text_filter
)

关于python - QFileDialog 为所有支持的图像格式创建过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54749648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com