[英]How can I get a QFileDialog to prompt for overwrite?
I have a QDialog that opens a QFileDialog like so:我有一个 QDialog 可以像这样打开一个 QFileDialog:
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.首先调用
setConfirmOverwrite(true)
或setOption(QFileDialog::DontConfirmOverwrite, false)
也无济于事。 I've tested this both on Qt 4.7.3 and 4.7.4 on both Ubuntu 11.04 and Windows XP.我已经在 Ubuntu 11.04 和 Windows XP 上的 Qt 4.7.3 和 4.7.4 上对此进行了测试。
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. QFileDialog::getSaveFileName()
有这个问题,但它是特定于 Maemo 的,并且在 Qt 4.7.3 出现之前修复得很好。 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.) (出于不相关的原因,我不想使用
getSaveFileName()
。)
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.
我认为这可能是由于对话框不知道它是否只是一个简单的打开对话框,提示没有意义,但我没有看到一种方法来告诉它它是一个保存对话框(除了设置确认覆盖选项,失败),并且文档确实说它应该默认提示。
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);
你可以通过调用
fd.setAcceptMode(QFileDialog::AcceptSave);
来做到这一点fd.setAcceptMode(QFileDialog::AcceptSave);
in your code example.在您的代码示例中。 See QFileDialog::acceptMode .
请参阅QFileDialog::acceptMode 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.