in a Qt dialog for saving a file, I have multiple options for file filters.
For instance, my filter is "Text Files (*.txt, *.pdf);;TXT Files (*.txt);;PDF files(*.pdf)"
.
This is my code:
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);
Now, the user may not introduce a file extension, so it should be the dialog's responsibility to force one based on the selected file filter.
For the case of multiple extensions, I could use QDialog::setDefaultSuffix()
, but this would not suffice because if I set the default suffix to, for instance, .pdf, the user may select the TXT files (*.txt)
filter, and I would override the user decision.
How can I force the file extension based on the selected filter?
Thank you
There is also the QFileDialod::selectedNameFilter()
, but basing my code on this would force me to check for the existence of the file afterwards, for not overriding (Is there a way to automatically add extensions to a file using QFileDialog on Linux)