I would like to filter the files that are shown in a QFileDialog
more specifically than just by file extensions. The examples I found in the Qt documentation only show filters like Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)
and such. In addition to this I would also like to specify a filter for files that should not show up in the file dialog, e.g. XML files (*.xml)
but not Backup XML files (*.backup.xml)
.
So the problem I have is that I would like to show some files in the file dialog that have certain file extension, but I would not like to show other files with a specific file name suffix (and the same file extension).
For example:
Files to show:
file1.xml
file2.xml
Files not to show:
file1.backup.xml
file2.backup.xml
I would like to ask if it is possible to define filters like these for a QFileDialog
?
I believe what you can do is:
Below is an example:
Proxy model:
dialog was created this way:
The proxy model is supported by non-native file dialogs only.
Okay, I've used it with QFileDialog object. And this only shows me the files listed in the appropriate directory. It is excellent to just choose the files to be processed. For example, an XML file, a PNG image, etcetera.
Here I present my example
The dialog box will display only presents xml files.
The solution of @serge_gubenko is working well. Create your own
ProxyModel
by inheriting from theQSortFilterProxyModel
.Just make sure to set
DontUseNativeDialog
before setting the Proxy model (not the way that @serge_gubenko did it). Native dialogs do not support customProxyModel
s.It took quite some time for me to find this out. This was written here