How to get overwrite result from QFileDialog
-
I have a save file function that uses the standard QFileDialog box to get the output file name. If the user selects a file that already exists, a query box pops up to confirm that the user wants to overwrite the selected file. However, I can't seem to find any way to get the results of that choice. I assume this is pretty simple and I'm missing something obvious, but I'm definitely missing it. Any help is appreciated.
-
@djsuson
You don't/shouldn't get the result of this. It is internal to the Save Dialog. If the user picks an existing file to save to the dialog verifies with them that they want to overwrite. You can assume the dialog only returns the filepath to you if/when the user has confirmed. -
That was what I needed to know. I combined this with QFileInfo to find out of the chosen file exists or is new. I need to modify the name for use with another library if I'm overwriting a previously existing file. Thank you for the help . I'll mark this as closed.
-
@djsuson
That's OK, but your user is going to be presented with a rather odd/confusing/untrue prompt. If they pick an existing from your save dialog it will put up a message saying "Are you sure you want to overwrite this file?". They say "Yes", but in fact you then alter that filename to save to a different file and not overwrite the file they picked.If you want to be more "truthful" to the user you might consider setting the QFileDialog::Options you pass to
getSaveFileName()
to includeQFileDialog::DontConfirmOverwrite
Don't ask for confirmation if an existing file is selected. By default confirmation is requested.because you won't actually be overwriting the file they selected. You might also tell them if you amend their (existing) selected file to something else.
-
Hi,
Just a recommendation: you should put a note in your code about that requirement otherwise there will likely be someone in the future (yourself included) that will wonder why there's that ! char there.