Qt Multiple file filters, force one extension
<p>in a Qt dialog for saving a file, I have multiple options for file filters.</p>

<p>For instance, my filter is <code>"Text Files (*.txt, *.pdf);;TXT Files (*.txt);;PDF files(*.pdf)"</code>.</p>

<p>This is my code:</p>

<pre><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);
</code></pre>

<p>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.</p>

<p>For the case of multiple extensions, I could use <code>QDialog::setDefaultSuffix()</code>, but this would not suffice because if I set the default suffix to, for instance, .pdf, the user may select the <code>TXT files (*.txt)</code> filter, and I would override the user decision.</p>

<p>How can I force the file extension based on the selected filter? </p>

<p>Thank you</p>

<hr>

<p>There is also the <code>QFileDialod::selectedNameFilter()</code>, but basing my code on this would force me to check for the existence of the file afterwards, for not overriding (<a href="https://stackoverflow.com/questions/9822177/is-there-a-way-to-automatically-add-extensions-to-a-file-using-qfiledialog-on-li">Is there a way to automatically add extensions to a file using QFileDialog on Linux</a>)</p>