I would like to open a QFileDialog.getOpenFileName
with all supported image formats (all file types that I could used to instantiate a QIcon
)
I already know that I can get all supported image formats with QImageReader.supportedImageFormats()
.
What confuses me is that QImageReader.supportedImageFormats()
returns a list of QBytesArray
, and I'm not sure how to convert this simply into a list of 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
You have to convert the QByteArray
to bytes
using the data()
method, then the bytes to string
using decode()
. Then it is only concatenated to obtain the required format.
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
)