QFileDialog prompt to overwrite if selection changed

463 views Asked by At

I would like a QFileDialog to prompt for overwrite confirmation only if the selection changed. Is there any way to achieve this?

Currently, I'm getting a save file name as follows:

data_file_name = QtGui.QFileDialog.getSaveFileName(self, "Data File", selected, self.FILTER_DATA_FILE)

If I'm re-saving an open project, selected is the name of the project file. If I'm creating a new project, selected is the name of the directory where the user most recently saved a project. If I enter a file name that doesn't exist (regardless of the value of selected), I don't get prompted for confirmation, as desired. However, if the selected name is a file that exists, also regardless of the value of selected, I always get promoted for confirmation. I would like the following behaviour:

  1. If the dialog has a default selection and the user does not change this selection, do not prompt for confirmation.
  2. Else if the user enters a file name that does not currently exist, do not prompt for confirmation.
  3. Else prompt for confirmation. (I don't particularly care what happens if the user enters the empty string as a file name.)

I'm currently using Qt 4, though I'm not against upgrading to Qt 5 if that would make my life easier.

1

There are 1 answers

0
user3553031 On

It looks like what I want isn't possible: QFileDialog::accept() doesn't provide any mechanism for that sort of customization. I'll have to disable overwrite confirmation in my dialog and implement that logic myself.