简体   繁体   English

如何让 QFileDialog 提示覆盖?

[英]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.

相关问题 如果选择更改,QFileDialog提示覆盖 - QFileDialog prompt to overwrite if selection changed 如何在Qt5中获得系统QFileDialog? - How do I get the system QFileDialog in Qt5? 如何在QFileDialog上获取大图标? - How to get big icons on QFileDialog? 如何禁用或隐藏 QFileDialog 中的默认取消按钮? - How can i disable or hide default cancel button in QFileDialog? QFileDialog作为TableView的编辑器:如何获得结果? - QFileDialog as editor for TableView: how to get result? 在 QWebView 中单击文件输入时,如何找到弹出的 QFiledialog? - How can I find the popped up QFiledialog when I click file input in the QWebView? 如何在非本机QFileDialog中重新填充系统的“最近地点”? - How can I repopulate the system's “Recent places” in non-native QFileDialog? 在使用QFileDialog的getExistingDirectory的QT中,如何在环境变量指定的目录中启动? - In QT using QFileDialog's getExistingDirectory, how can I start in a directory specified by an environment variable? 如果函数 raise()、activeWindow() 和其他函数不起作用,我如何将 QFileDialog 提升到前台? - How i can to raise QFileDialog to the foreground if functions raise(),activeWindow() and other didn't work? 如何覆盖QWizard中的“下一个”插槽? - How can I overwrite the “next” slot in a QWizard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM