如何让 QFileDialog 提示覆盖?
我有一个 QDialog,可以像这样打开 QFileDialog:
QFileDialog fd(this);
fd.setFileMode(QFileDialog::AnyFile);
if (fd.exec()) {
// save data to a file
}
不幸的是, 默认行为似乎并不那么默认,并且如果我选择已经存在的文件,文件对话框不会提示我覆盖。调用 setConfirmOverwrite(true)
< /a> 或 setOption(QFileDialog::DontConfirmOverwrite, false)
首先也没有帮助。我已经在 Ubuntu 11.04 和 Windows XP 上的 Qt 4.7.3 和 4.7.4 上进行了测试。
我环顾四周,发现了此错误报告。 QFileDialog::getSaveFileName()
有这个问题,但它是 Maemo 特有的,并且在 Qt 4.7.3 发布之前就已修复。如果我在应用程序中使用该方法,它工作得很好,系统会提示我覆盖该文件。 (出于不相关的原因,我不想使用 getSaveFileName() 。)
我找不到其他人抱怨这对他们不起作用。我做错了什么,还是这是一个错误?我认为这可能是由于对话框不知道它是否只是一个简单的打开对话框,其中提示没有意义,但我没有找到一种方法来告诉它它是一个保存对话框(除了设置确认覆盖选项之外,失败),并且文档确实说它应该默认提示。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还应该确保该对话框处于保存模式,因为它不会认为您在打开模式下覆盖文件。您可以通过在代码示例中调用
fd.setAcceptMode(QFileDialog::AcceptSave);
来完成此操作。请参阅QFileDialog::acceptMode。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.