Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileDialog doesn't add selected extension
Forum Updated to NodeBB v4.3 + New Features

QFileDialog doesn't add selected extension

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 7 Posters 3.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Publicnamer

    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.

    A Offline
    A Offline
    anil_arise
    wrote on last edited by anil_arise
    #5

    @Publicnamer
    Use this for current selected extension

    QString extension = fileDialog.selectedNameFilter();

    Details SelectedNameFilter

    P 1 Reply Last reply
    1
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #6

      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
      
      JonBJ 1 Reply Last reply
      3
      • C ChrisW67

        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
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #7

        @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?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #8

          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.

          P 1 Reply Last reply
          1
          • M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #9

            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.

            artwawA P 2 Replies Last reply
            0
            • A anil_arise

              @Publicnamer
              Use this for current selected extension

              QString extension = fileDialog.selectedNameFilter();

              Details SelectedNameFilter

              P Offline
              P Offline
              Publicnamer
              wrote on last edited by
              #10

              @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.

              1 Reply Last reply
              0
              • C ChrisW67

                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.

                P Offline
                P Offline
                Publicnamer
                wrote on last edited by Publicnamer
                #11

                @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.

                1 Reply Last reply
                0
                • M mpergand

                  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.

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on last edited by
                  #12

                  @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.

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  1 Reply Last reply
                  0
                  • M mpergand

                    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.

                    P Offline
                    P Offline
                    Publicnamer
                    wrote on last edited by
                    #13

                    @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.

                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • P Publicnamer

                      @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.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      @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

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • P Publicnamer

                        @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.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #15

                        @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.

                        1 Reply Last reply
                        2
                        • P Publicnamer

                          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.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #16

                          @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.

                          P 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @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.

                            P Offline
                            P Offline
                            Publicnamer
                            wrote on last edited by
                            #17

                            @JonB I just saw your message. Yes I've found that QFileDialog::getSaveFileName does not exhibit the bug, so I've switched to that.

                            JonBJ 1 Reply Last reply
                            0
                            • P Publicnamer

                              @JonB I just saw your message. Yes I've found that QFileDialog::getSaveFileName does not exhibit the bug, so I've switched to that.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #18

                              @Publicnamer
                              Interesting. I'm glad it has worked fro you, but puzzled. The code can be seen in https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qfiledialog.cpp.html#_ZN11QFileDialog15getSaveFileNameEP7QWidgetRK7QStringS4_S4_PS2_6QFlagsINS_6OptionEE . That just calls https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qfiledialog.cpp.html#_ZN11QFileDialog14getSaveFileUrlEP7QWidgetRK7QStringRK4QUrlS4_PS2_6QFlagsINS_6OptionEERK11QStringList . That effectively just goes

                                  if (dialog.exec() == QDialog::Accepted) {
                                      if (selectedFilter)
                                          *selectedFilter = dialog.selectedNameFilter();
                                      return dialog.selectedUrls().value(0);
                                  }
                              

                              Quite why that would append an extension to the returned file/URL, where your own original code did not, I do not know.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved