QFileDialog: adding extension automatically when saving file?
Asked Answered
C

2

16

When using a QFileDialog to save a file and to specify the extension (like *.pdf) and the user types in a name without this extension, also the saved file hasn't this extension.
Example-Code:

QFileDialog fileDialog(this, "Choose file to save");
fileDialog.setNameFilter("PDF-Files (*.pdf)");
fileDialog.exec();
QFile pdfFile(fileDialog.selectedFiles().first());

now when the user enters "foo" as the name, the file will be saved as "foo", not as "foo.pdf". So the QFileDialog doesn't add the extension automatically. My question: How can I change this?

Collotype answered 23/12, 2009 at 16:1 Comment(4)
Qt documentation is clear and comprehensive, I'm wondering how could you possibly have missed QFileDialog::setDefaultSuffix()Hoehne
Looks like a lot of works just to do the same as : QFileDialog::getSaveFileName(). Do I miss something ?Scheller
@Julien L.: I usually try to answer the question, not question the need, especially for simple queries like this. However, if the OP would have opened the page I linked to and searched for "extension", he would have found only the example for "setNameFilter", and nothing else related to default suffixes.Microphyte
@cjhuitt: Exactly, that's the reason I asked this (easy to answer) questionCollotype
M
21

You could use QFileDialog::setDefaultSuffix():

Suffix added to the filename if no other suffix was specified.

This property specifies a string that is added to the filename if it has no suffix yet. The suffix is typically used to indicate the file type (e.g. "txt" indicates a text file).

If the first character is a dot ('.'), it is removed.

Microphyte answered 23/12, 2009 at 16:21 Comment(2)
How can this be done when there are multiple filetypes allowed? e.g, "All-Files (.pdf *.txt);;PDF-Files(.pdf)" and the user selectd the second entry, PDF-Files?Gleeman
@Gleeman While not exactly trivial, it should be possible to subclass QFileDialog, connect the filterSelected() signal to a slot which searches the filter string for the first file extension, and then calls setDefaultSuffix() with it.Crick
I
2

For multiple file filters, the following can be done.

import re
import os

def saveFile(self):
    path, fileFilter = QFileDialog().getSaveFileName(self, "Save file", 
        "", "Gnuplot Files (*.plt)" 
        + ";;" + "Gnuplot Files (*.gp)"
        + ";;" + "Gnuplot Files (*.gpt)"
        + ";;" + "Text Files (*.txt)")

    selectedExt = re.search('\((.+?)\)',fileFilter).group(1).replace('*','')

    # Attach extension as per selected filter, 
    # if file does not have extension.
    if not os.path.splitext(path)[1]:
        path = path + selectedExt

    print(path)
Inartificial answered 23/11, 2021 at 17:34 Comment(1)
This simply adds the extension after the dialog is closed so will not work in sandboxed environments (Android, maybe Mac also) where write access may only be granted to the file exactly as selected by the dialog.Acknowledge

© 2022 - 2024 — McMap. All rights reserved.