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. How do I limit the files that are shown in QFileDialog?
QtWS25 Last Chance

How do I limit the files that are shown in QFileDialog?

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 8.3k Views
  • 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.
  • T Offline
    T Offline
    technovelist
    wrote on last edited by
    #1

    I have a program that wants to save files with a specific extension. This works as expected if there are no files having the same name as the file I want to save, but if there are files with the same name (or name prefix) and different extensions, it shows all the files that match on the name.

    What I want to do is to show only those files that have the desired extension, not any other files with the same name or name prefix.

    For example, suppose that the extension is ".rra" and there are the following files in the directory: tm001.rra, tm001.csv, tm001.txt, tm001.rrr. What I get when I type "tm001" into the save dialog is the list of all those files. What I want is only "tm001.rra".

    I'm sure there is some way to do this but I haven't been able to figure out how to do it. Can anyone point me to an example or explain how that file list is constructed so I can modify it?

    Thanks.

    1 Reply Last reply
    0
    • O Offline
      O Offline
      Olivier Ronat
      wrote on last edited by
      #2

      Hello
      I think you must use the QFileDialog Method setNameFilter(const QString & filter)

      For instance
      QFileDialog dialog(this);
      dialog.setNameFilter("*.rra");
      dialog.exec();

      1 Reply Last reply
      0
      • T Offline
        T Offline
        technovelist
        wrote on last edited by technovelist
        #3

        Nope, I've tried that. Here's the code in that function. The FileFilter string has a value like "My Program Name (*.rra)" after the first statement.

        string FileFilter(ProgramName + " (*." + ParameterFileExtension + ")");
        
        QFileDialog dialog(this);
        dialog.setFileMode(QFileDialog::AnyFile);
        dialog.setNameFilter(FileFilter.c_str());
        dialog.setDefaultSuffix(ParameterFileExtension.c_str());
        QStringList fileNames;
        if (dialog.exec())
        	fileNames = dialog.selectedFiles();
        

        What am I missing?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Are you sure your name filter is valid ?

          By the way, why not use QString ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Are you sure your name filter is valid ?

            By the way, why not use QString ?

            T Offline
            T Offline
            technovelist
            wrote on last edited by
            #5

            The NameFilter value is the same, "My Program Name (*.rra)". Is that not valid?
            And as for why I don't use QString, I generally use standard C++ types if there is no pressing reason not to use them. Would that make a difference?

            1 Reply Last reply
            0
            • O Offline
              O Offline
              Olivier Ronat
              wrote on last edited by
              #6

              Perhaps the problem is the string QString conversion.
              So create QString for FileFilter and ParameterFileExtension or use QString::fromStdString(FIleFilter)

              1 Reply Last reply
              0
              • T Offline
                T Offline
                technovelist
                wrote on last edited by
                #7

                Nope. This has the same behavior:

                QString FileFilter(QString::fromStdString(ProgramName + " (*." + ParameterFileExtension + ")"));
                QString PFE(QString::fromStdString(ParameterFileExtension));
                
                QFileDialog dialog(this);
                dialog.setFileMode(QFileDialog::AnyFile);
                dialog.setNameFilter(FileFilter);
                dialog.setDefaultSuffix(PFE);
                QStringList fileNames;
                if (dialog.exec())
                	fileNames = dialog.selectedFiles();
                

                Any other suggestions would be welcome.

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  Olivier Ronat
                  wrote on last edited by
                  #8

                  What is literally the ParameterFileExtension String?

                  T 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    What OS/Qt version are you using ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    T 1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      Olivier Ronat
                      wrote on last edited by Olivier Ronat
                      #10

                      I have tested the code with QString, Everything is working well in Qt 5.5

                      QFileDialog dialog(this);
                      dialog.setAcceptMode(QFileDialog::AcceptSave);
                      dialog.setNameFilter("My Program Name(*.txt)");
                      dialog.setFileMode(QFileDialog::AnyFile);
                      dialog.setDefaultSuffix("txt");
                      QStringList fileNames;
                      if (dialog.exec())
                          fileNames = dialog.selectedFiles();
                      
                      1 Reply Last reply
                      0
                      • SGaistS SGaist

                        What OS/Qt version are you using ?

                        T Offline
                        T Offline
                        technovelist
                        wrote on last edited by
                        #11

                        @SGaist OS = Win 7 Ultimate, x64, Qt = 5.4.3 open source

                        1 Reply Last reply
                        0
                        • O Olivier Ronat

                          What is literally the ParameterFileExtension String?

                          T Offline
                          T Offline
                          technovelist
                          wrote on last edited by
                          #12

                          @Olivier-Ronat "rra"

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            technovelist
                            wrote on last edited by technovelist
                            #13

                            Obviously I'm not explaining my issue sufficiently.

                            Here's a picture of what I'm seeing when I run the exact code that Olivier Ronat provided:

                            www.steveheller.org/misc/QtIssue.png

                            What I want to see in the dropdown is ONLY those files having the rra extension, not any files with any other extensions. So when I have typed "tm000", the dropdown should show **only ** tm000.rra, no other files.

                            I'm sure there is some way to achieve this, given how flexible Qt is. Can someone help me do this?

                            Thanks.

                            JKSHJ 1 Reply Last reply
                            0
                            • T technovelist

                              Obviously I'm not explaining my issue sufficiently.

                              Here's a picture of what I'm seeing when I run the exact code that Olivier Ronat provided:

                              www.steveheller.org/misc/QtIssue.png

                              What I want to see in the dropdown is ONLY those files having the rra extension, not any files with any other extensions. So when I have typed "tm000", the dropdown should show **only ** tm000.rra, no other files.

                              I'm sure there is some way to achieve this, given how flexible Qt is. Can someone help me do this?

                              Thanks.

                              JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by JKSH
                              #14

                              @technovelist said:

                              What I want to see in the dropdown is ONLY those files having the rra extension, not any files with any other extensions. So when I have typed "tm000", the dropdown should show **only ** tm000.rra, no other files.

                              That behaviour is built into Windows itself. The Windows dialog only uses the name filter on the large list at the top, not in the drop-down menu.

                              Qt simply asks Windows to show you its file selection dialog by default

                              I'm sure there is some way to achieve this, given how flexible Qt is. Can someone help me do this?

                              There is. Call dialog.setOptions(QFileDialog::DontUseNativeDialog); to get a custom Qt dialog which also applies your name filter to the drop-down menu. It's not as pretty as the Windows dialog, though.

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              T 1 Reply Last reply
                              0
                              • JKSHJ JKSH

                                @technovelist said:

                                What I want to see in the dropdown is ONLY those files having the rra extension, not any files with any other extensions. So when I have typed "tm000", the dropdown should show **only ** tm000.rra, no other files.

                                That behaviour is built into Windows itself. The Windows dialog only uses the name filter on the large list at the top, not in the drop-down menu.

                                Qt simply asks Windows to show you its file selection dialog by default

                                I'm sure there is some way to achieve this, given how flexible Qt is. Can someone help me do this?

                                There is. Call dialog.setOptions(QFileDialog::DontUseNativeDialog); to get a custom Qt dialog which also applies your name filter to the drop-down menu. It's not as pretty as the Windows dialog, though.

                                T Offline
                                T Offline
                                technovelist
                                wrote on last edited by technovelist
                                #15

                                @JKSH said:

                                @technovelist said:

                                What I want to see in the dropdown is ONLY those files having the rra extension, not any files with any other extensions. So when I have typed "tm000", the dropdown should show **only ** tm000.rra, no other files.

                                That behaviour is built into Windows itself. The Windows dialog only uses the name file on the large list at the top, not in the drop-down menu.

                                Qt simply asks Windows to show you its file selection dialog by default

                                I'm sure there is some way to achieve this, given how flexible Qt is. Can someone help me do this?

                                There is. Call dialog.setOptions(QFileDialog::DontUseNativeDialog); to get a custom Qt dialog which also applies your name filter to the drop-down menu. It's not as pretty as the Windows dialog, though.

                                Thanks, that's what I was looking for. You weren't kidding about its not being pretty though. :-(

                                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