Force Filename in Save File Dialog

  • Sorry that I have to make a post about this but I surprisingly was not able to find anything about this with Google or the documentation. Maybe I just wasn't using the right keywords.

    I have the current line in my code.

    QString qsINIPath = QFileDialog::getSaveFileName(this,tr("Select name/directory to save the filepath configuration"), QDir::homePath(), tr("Config (*.ini)"));

    which works perfectly fine in creating a QString for the location of this file the user wants to save. What I would like to do however is keep the name of this configuration .ini the same always. I have seen this done on other programs before where a file save dialog comes up with a fixed file name that the program doesn't let you change and you essentially only pick a location.

    I know I could do this by using a directory browse window instead and then appending the file name to the end of the QString before passing it to my write function. I would like to avoid this however as I want it to be more clear that a file is being saved (hence the save button and the fact its a file dialog not a directory dialog), and am confident it can be done given past exposure to other programs, though maybe I am just remembering wrong.

    I did realize that it may be possible to just bring up the save dialog and then right after change the string in the filename field and then set the field to not editable. I am not sure how I would do this though as I wouldn't know how to refer to the dialog object and the objects within it.

    Does anyone know how to do this? Have the same thing as a save file dialog but with a forced filename?


  • Moderators

    You could try retrieving the line edit for the file name and setting a mask that doesn't allow the name to be changed. This however will not work with native dialogs.

  • You could check the name returned from QFileDialog and alter it so that it always uses a specific name regardless of what the user had selected or entered. Basically you are only using the path part of the file name.

    This is a bad practice generally speaking as this can be confusing to the user. A directory dialog would be better in this case or letting the user know the change in the selected file name has occurred; at least they will know why the file they chose is not the one used.

    For configuration files (INI files) this should never be something the user is required to select. They should be loaded automatically when the application starts (from a established location like '~/.app_data') and saved when closed and possibly updated as needed while the program is running. Since you want to use a fixed file name it sounds like the purpose of this is settings for the running application and not something like optional parameters or configurations (?). I wouldn't even prompt the user for stuff like this if this assumption is true.

  • The standard way to pick file location is to pick a folder with:

    If you want to have an exact look of file dialog with disabled edit control and some other features you may subclass QFileDialog. If you need native dialog you will have to write system specific code and use it instead (For example it is possible under Windows, but customization of native Windows dialog may be not a trivial task for a person without Windows programming experience.)

  • @Rondog @alex_malyu

    I see that this is not as simple as I had hoped and I guess I will just deal with using a directory browse or letting the user set the name. The reason i have the user interacting with an .ini is because it is not just global program settings that are loaded. I want the user to have multiple configurations that they can easily switch between that have file paths and numeric settings saved so that the program can be used easily in rapid succession. The GUI is a port of a console application that is not yet finished so I want debugging with it to be quick. This is why I have the ability to save and load a .ini file so that if a user keeps testing with the same paths over and over they don't need to keep browsing for the same files over and over again. However, I wanted there to be the ability to have multiple setups saved which is why I don't just want there to be one .ini that is used automatically.

    I know I could use some kind of slot system with the same .ini (like saves lots on a video game) to accomplish the same thing and the user just picks one of those slots. But like I said the application is not finished yet (the code behind the GUI) so it being perfectly clean is not an issue at the moment.

    Thank you for the information.