setting default file extension when creating a new file...
-
Is there a way to have the file browser add a extension on the end of the file that does not have one when creating a new file?
For example in my app the settings always put a .emp file extension when a save/save-as is used for a new file. Or should I add it on the end after the browser returns the filename?
-
The return value of a file dialog in Qt is a file name. It doesn’t create or open a file. You can set name or mime type filters, e.g. to limit or exclude suffixes. You can modify the return value before creating a file, even exchanging one suffix with another.
-
@Axel-Spoerl ah cool, thanks. I will put in code to check if the filename ends in .emp and add it if not, then save to that file.
Is there a way to have my filter default to "on" rather than a option? I have a filter that lists files of my configured type, for instance *.emp, but the file browser defaults to showing all files. -
If you have specified a filter it should be applied. However, this usually occurs when you are opening (an existing file), when saving a file it's not so important (only relevant to show you what files you might overwrite, but of course unlike when opening you do not have to pick one). Behaviour might depend on whether you are using the native File Browser or the Qt one, and which platform you are on.
-
I don't know what "Haiku" is as an operating system. I can only say that under Ubuntu 22.04 with Qt5 (all I have)
QFileDialog::getSaveFileName(&w, "Caption", QString(), "*.cpp"); // or QFileDialog::getSaveFileName(&w, "Caption", QString(), "*.cpp", nullptr, QFileDialog::DontUseNativeDialog);
These use native dialog/Qt dialog respectively. Both apply the
*.cpp
filter from the outset, and neither has a "show all" filter. -
@MarisaG
Yep same as mine. Just try*.emp
in place ofEmpire Config Files (*.emp)
, though it should be same.I think actually you are using your OS' native dialog, and that may behave differently (and I don't know whether you can affect it). Try my one with
QFileDialog::DontUseNativeDialog
and see how that compares/behaves. -
@MarisaG said in setting default file extension when creating a new file...:
I have the filter, and if I manually choose it it gets applied, but I would like the filter to be applied by default and the "show all" filter to be optional.
Try:
QString fileName = QFileDialog::getOpenFileName(0L, "Open Empire Config","", "Empire Config Files (*.emp) ;; All Files (*)");
-
@MarisaG said in setting default file extension when creating a new file...:
Hmm :( Maybe i will have to ask in the haiku forums for people who have experience with qt...
Could be "Haiku" related behavior.
Have you tried @JonB 's suggestion here:@JonB said in setting default file extension when creating a new file...:
Try my one with QFileDialog::DontUseNativeDialog and see how that compares/behaves.
Maybe Haiku adds "All files" by default or so. Don't know. Usually, on Windows, Linux and even Mac (AFAIK) you don't have "All files", when you set a filter which excludes it.
-
@Pl45m4 I do want it to look like a native app, so i like the way this is working other than the file list. I rearranged my file layout so that it defaults to looking for the config files for empclient under the system config area so this is not as much a issue anymore.
I'm sure i will have more questions LOL.
-
@MarisaG said in setting default file extension when creating a new file...:
I do want it to look like a native app, so i like the way this is working other than the file list
Yes, understood. But have you tried it? :)
If you try Qt's FileDialog (QFileDialog::DontUseNativeDialog
flag set) and there is only one "All Files" left, then you know for sure that it is Haiku's default file dialog that has its own "All Files" (which Qt has no control over).