我想打开一个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
,然后使用decode()
将字节转换为string
。然后将其串联起来以获得所需的格式。你知道吗相关问题 更多 >
编程相关推荐