How do I limit the files that are shown in QFileDialog?
-
I have a program that wants to save files with a specific extension. This works as expected if there are no files having the same name as the file I want to save, but if there are files with the same name (or name prefix) and different extensions, it shows all the files that match on the name.
What I want to do is to show only those files that have the desired extension, not any other files with the same name or name prefix.
For example, suppose that the extension is ".rra" and there are the following files in the directory: tm001.rra, tm001.csv, tm001.txt, tm001.rrr. What I get when I type "tm001" into the save dialog is the list of all those files. What I want is only "tm001.rra".
I'm sure there is some way to do this but I haven't been able to figure out how to do it. Can anyone point me to an example or explain how that file list is constructed so I can modify it?
Thanks.
-
Hello
I think you must use the QFileDialog Method setNameFilter(const QString & filter)For instance
QFileDialog dialog(this);
dialog.setNameFilter("*.rra");
dialog.exec(); -
Nope, I've tried that. Here's the code in that function. The FileFilter string has a value like "My Program Name (*.rra)" after the first statement.
string FileFilter(ProgramName + " (*." + ParameterFileExtension + ")"); QFileDialog dialog(this); dialog.setFileMode(QFileDialog::AnyFile); dialog.setNameFilter(FileFilter.c_str()); dialog.setDefaultSuffix(ParameterFileExtension.c_str()); QStringList fileNames; if (dialog.exec()) fileNames = dialog.selectedFiles();
What am I missing?
-
Hi,
Are you sure your name filter is valid ?
By the way, why not use QString ?
-
The NameFilter value is the same, "My Program Name (*.rra)". Is that not valid?
And as for why I don't use QString, I generally use standard C++ types if there is no pressing reason not to use them. Would that make a difference? -
Perhaps the problem is the string QString conversion.
So create QString for FileFilter and ParameterFileExtension or use QString::fromStdString(FIleFilter) -
Nope. This has the same behavior:
QString FileFilter(QString::fromStdString(ProgramName + " (*." + ParameterFileExtension + ")")); QString PFE(QString::fromStdString(ParameterFileExtension)); QFileDialog dialog(this); dialog.setFileMode(QFileDialog::AnyFile); dialog.setNameFilter(FileFilter); dialog.setDefaultSuffix(PFE); QStringList fileNames; if (dialog.exec()) fileNames = dialog.selectedFiles();
Any other suggestions would be welcome.
-
What is literally the ParameterFileExtension String?
-
What OS/Qt version are you using ?
-
I have tested the code with QString, Everything is working well in Qt 5.5
QFileDialog dialog(this); dialog.setAcceptMode(QFileDialog::AcceptSave); dialog.setNameFilter("My Program Name(*.txt)"); dialog.setFileMode(QFileDialog::AnyFile); dialog.setDefaultSuffix("txt"); QStringList fileNames; if (dialog.exec()) fileNames = dialog.selectedFiles();
-
@SGaist OS = Win 7 Ultimate, x64, Qt = 5.4.3 open source
-
What is literally the ParameterFileExtension String?
@Olivier-Ronat "rra"
-
Obviously I'm not explaining my issue sufficiently.
Here's a picture of what I'm seeing when I run the exact code that Olivier Ronat provided:
www.steveheller.org/misc/QtIssue.png
What I want to see in the dropdown is ONLY those files having the rra extension, not any files with any other extensions. So when I have typed "tm000", the dropdown should show **only ** tm000.rra, no other files.
I'm sure there is some way to achieve this, given how flexible Qt is. Can someone help me do this?
Thanks.
-
Obviously I'm not explaining my issue sufficiently.
Here's a picture of what I'm seeing when I run the exact code that Olivier Ronat provided:
www.steveheller.org/misc/QtIssue.png
What I want to see in the dropdown is ONLY those files having the rra extension, not any files with any other extensions. So when I have typed "tm000", the dropdown should show **only ** tm000.rra, no other files.
I'm sure there is some way to achieve this, given how flexible Qt is. Can someone help me do this?
Thanks.
@technovelist said:
What I want to see in the dropdown is ONLY those files having the rra extension, not any files with any other extensions. So when I have typed "tm000", the dropdown should show **only ** tm000.rra, no other files.
That behaviour is built into Windows itself. The Windows dialog only uses the name filter on the large list at the top, not in the drop-down menu.
Qt simply asks Windows to show you its file selection dialog by default
I'm sure there is some way to achieve this, given how flexible Qt is. Can someone help me do this?
There is. Call
dialog.setOptions(QFileDialog::DontUseNativeDialog);
to get a custom Qt dialog which also applies your name filter to the drop-down menu. It's not as pretty as the Windows dialog, though. -
@technovelist said:
What I want to see in the dropdown is ONLY those files having the rra extension, not any files with any other extensions. So when I have typed "tm000", the dropdown should show **only ** tm000.rra, no other files.
That behaviour is built into Windows itself. The Windows dialog only uses the name filter on the large list at the top, not in the drop-down menu.
Qt simply asks Windows to show you its file selection dialog by default
I'm sure there is some way to achieve this, given how flexible Qt is. Can someone help me do this?
There is. Call
dialog.setOptions(QFileDialog::DontUseNativeDialog);
to get a custom Qt dialog which also applies your name filter to the drop-down menu. It's not as pretty as the Windows dialog, though.@JKSH said:
@technovelist said:
What I want to see in the dropdown is ONLY those files having the rra extension, not any files with any other extensions. So when I have typed "tm000", the dropdown should show **only ** tm000.rra, no other files.
That behaviour is built into Windows itself. The Windows dialog only uses the name file on the large list at the top, not in the drop-down menu.
Qt simply asks Windows to show you its file selection dialog by default
I'm sure there is some way to achieve this, given how flexible Qt is. Can someone help me do this?
There is. Call
dialog.setOptions(QFileDialog::DontUseNativeDialog);
to get a custom Qt dialog which also applies your name filter to the drop-down menu. It's not as pretty as the Windows dialog, though.Thanks, that's what I was looking for. You weren't kidding about its not being pretty though. :-(