【发布时间】:2016-05-30 21:13:49
【问题描述】:
在用于保存文件的 Qt 对话框中,我有多个文件过滤器选项。
例如,我的过滤器是"Text Files (*.txt, *.pdf);;TXT Files (*.txt);;PDF files(*.pdf)"。
这是我的代码:
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setNameFilter(filter.c_str());
dialog.setAcceptMode( QFileDialog::AcceptSave );
dialog.setWindowTitle(WINDOW_TITLE_EXPORT_CLOUD);
if (!dialog.exec())
return;
QString fileName;
auto fileName = dialog.selectedFiles().at(0);
现在,用户可能不会引入文件扩展名,因此对话框应负责根据所选文件过滤器强制一个。
对于多个扩展的情况,我可以使用QDialog::setDefaultSuffix(),但这还不够,因为如果我将默认后缀设置为,例如,.pdf,用户可以选择TXT files (*.txt)过滤器,我会覆盖用户的决定。
如何根据所选过滤器强制文件扩展名?
谢谢
还有QFileDialod::selectedNameFilter(),但基于此我的代码将迫使我事后检查文件是否存在,以免覆盖 (Is there a way to automatically add extensions to a file using QFileDialog on Linux)
【问题讨论】: