Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a QFileDialog to prompt for overwrite?

I have a QDialog that opens a QFileDialog like so:

QFileDialog fd(this);
fd.setFileMode(QFileDialog::AnyFile);

if (fd.exec()) {
    // save data to a file
}

Unfortunately, the default behavior doesn't seem to be quite so default, and the file dialog doesn't prompt me about overwriting if I select a file that already exists. Calling setConfirmOverwrite(true) or setOption(QFileDialog::DontConfirmOverwrite, false) first doesn't help either. I've tested this both on Qt 4.7.3 and 4.7.4 on both Ubuntu 11.04 and Windows XP.

I looked around and found this bug report. QFileDialog::getSaveFileName() had this issue, but it was specific to Maemo and fixed well before Qt 4.7.3 came out. If I use that method in my application it works just fine, I get prompted about overwriting the file. (I don't want to use getSaveFileName() for unrelated reasons.)

I can't find anyone else complaining about this not working for them. Am I doing something wrong, or is this a bug? I think it might be due to the dialog not knowing whether it's just a simple Open dialog where prompting wouldn't make sense, but I don't see a way to tell it it's a Save dialog (beyond setting the confirm-overwrite option, which fails), and the documentation does say it should prompt by default.

like image 931
Matthew Read Avatar asked Feb 14 '12 23:02

Matthew Read


1 Answers

You should also be sure that the dialog is in save mode, as it will not think you are overwriting a file when in open mode. You can do this by calling fd.setAcceptMode(QFileDialog::AcceptSave); in your code example. See QFileDialog::acceptMode.

like image 169
Michael Avatar answered Sep 24 '22 12:09

Michael