Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QFileDialog doesn't add selected extension



  • I'm trying to use QFileDialog to create a Save As popup.
    I can enter the filename e.g. "foo", but if I selected the "PNG (.png)" file type, the dialog doesn't add that extension for me.
    Is there a way to make it do that?
    Thanks.



  • @Publicnamer
    Does not show the extension-type in the filter selector? Does not append that extension in the returned file path? Or what?



  • @Publicnamer can you show your code (QFileDialog to create a Save As popup)?



  • It shows the available file types and extensions in the pull-down menu, but it does not append the extension.

        const QStringList filters({
                        "Thingy (*.thi)",
                        "Dingy (*.dni)"
                        });
        QFileDialog fileDialog(this);
        fileDialog.setAcceptMode(QFileDialog::AcceptSave);
        fileDialog.setNameFilters(filters);
        fileDialog.exec();
    
        if (!fileDialog.selectedFiles().count())
                return;
        QString filename = fileDialog.selectedFiles().first();
    

    I don't know why it isn't automatically appending the extension and I'm unsure how I can learn which extension the user selected.



  • @Publicnamer
    Use this for current selected extension

    QString extension = fileDialog.selectedNameFilter();

    Details SelectedNameFilter



  • This works as expected for me (Linux, Qt 5.15):

    #include <QApplication>
    #include <QFileDialog>
    #include <QDebug>
    
    int main(int argc, char *argv[]) {
        QApplication a(argc, argv);
    
        const QStringList filters({  "Thingy (*.thi)",  "Dingy (*.dni)"  });
        QFileDialog fileDialog;
        fileDialog.setDirectory("/tmp/testfolder");
        fileDialog.setAcceptMode(QFileDialog::AcceptSave);
        fileDialog.setNameFilters(filters);
        fileDialog.exec();
    
        if (fileDialog.selectedFiles().count())
            qDebug() << fileDialog.selectedFiles();
    
        return 0;
    }
    

    Run it, select Thingy file type, enterfred in the file name and this is the result:

    17:20:46: Starting /tmp/build-untitled-Desktop-Debug/untitled ...
    ("/tmp/testfolder/fred.thi")
    17:20:49: /tmp/build-untitled-Desktop-Debug/untitled exited with code 0
    

    or for Dingy:

    17:20:53: Starting /tmp/build-untitled-Desktop-Debug/untitled ...
    ("/tmp/testfolder/fred.dni")
    17:21:04: /tmp/build-untitled-Desktop-Debug/untitled exited with code 0
    


  • @ChrisW67 said in QFileDialog doesn't add selected extension:

    This works as expected for me

    That's what I thought ought be the case, because I thought somewhere the QFileDialog docs said it returns a full path to the selected filename....

    Since the OP does not bother to mention what platform he is on we usually have to guess that means Windows. Maybe that uses the native Windows dialog and returns a different result?



  • I went with Linux because many of the Op's other post refer to PinePhone, which runs Linux.

    I've used the dialog on Windows in the past without the issue described. Don't have a Windows dev environment to hand at the moment to test it with exactly the same Qt version.



  • I experienced similar issue with earlier version of Qt5 with linux

    if(fileDialog.exec())
    		{
    		QStringList list=fileDialog.selectedFiles();
            prefs.setValue(Prefs_SaveDialogLastPath,fileDialog.directoryUrl());
    
    		if(!list.isEmpty())
    			{
    			filename=list.at(0);
    			fileInfo.setFile(filename);
    			QString ext=fileInfo.suffix();
    
                if(ext.isEmpty())	// ajoute l'extension sélectionnée (pas automatique sous linux)
    				{
                    ext=fileDialog.selectedNameFilter();
                    int start=ext.lastIndexOf('.');
    
                    if(start>0)
                        {
                        ext=ext.mid(start,ext.length()-start-1);
                        if(!ext.isEmpty()) filename+=ext;
                        }
    				}
    			}
    		}
    

    Since i maneged this issue by myself, i don't know if it has been fixed since then.



  • @anil_arise That returns the full extension string. I'd have to parse it to extract the .thi or .dni.
    I wonder why Qt doesn't offer just the extension.



  • @ChrisW67 I'm using Linux. I'm using Qt 5.11.3 for aarch64.

    I find that if I add only one name filter then when I type a single letter e.g. "a", a drop-down appears that says "a.thi". If I ignore that and click Save, then dialog will end up giving me just /home/me/a and not /home/me/a.thi.
    I don't recall seeing that drop-down when I had more than one name filter.

    I can just manually add the extension after I get the first selectedFile, but if a file by the name a.thi already exists, the problem is the user will never see the fileDialog's usual "Really replace this file" type of pop-up.

    Thus I think this is just a Qt bug.



  • @mpergand said in QFileDialog doesn't add selected extension:

    QString ext=fileInfo.suffix();

    You will find it difficult to tell .tar.gz from .gz in example. I'd suggest using QFileInfo::completeSuffix() instead.



  • @mpergand said in QFileDialog doesn't add selected extension:

    ajoute l'extension sélectionnée (pas automatique sous linux)

    To summarize then my question is why isn't it automatic under Linux.
    I suppose I could search through the Qt source code to find out but I gather Qt has people on staff to fix bugs.


  • Lifetime Qt Champion

    @Publicnamer said in QFileDialog doesn't add selected extension:

    but I gather Qt has people on staff to fix bugs

    Yes, but not in this user forum.
    If you think there is a bug please file a bug in https://bugreports.qt.io/secure/Dashboard.jspa



  • @Publicnamer
    QFileDialog can appear in two different forms, a Qt dialog or a native dialog. See https://doc.qt.io/qt-5/qfiledialog.html#Option-enum QFileDialog::DontUseNativeDialog. Different platforms may use the Qt or the native version regardless in certain situations.

    I think one thing you should look at is whether in your case on your platform it is using the Qt or native dialog, and report back here for others to compare. Also if you are able to try with the opposite setting for QFileDialog::DontUseNativeDialog, if that produces the other version does that still exhibit the failure to append the extension? Behaviour may not differ, I don't know, but it could be relevant.



  • @Publicnamer
    Furthermore, you are using code to create a QFileDialog instance and using that. Which is fine. However there is also a static method for saving a file, https://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName. I think you should also test that for your situation and see whether the result is any different.


Log in to reply