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. setting default file extension when creating a new file...
Forum Updated to NodeBB v4.3 + New Features

setting default file extension when creating a new file...

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 1.0k Views 4 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.
  • MarisaGM Offline
    MarisaGM Offline
    MarisaG
    wrote on last edited by
    #1

    Is there a way to have the file browser add a extension on the end of the file that does not have one when creating a new file?

    For example in my app the settings always put a .emp file extension when a save/save-as is used for a new file. Or should I add it on the end after the browser returns the filename?

    --- https://beos.retro-os.live- support and community for BeOS & Haiku

    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      The return value of a file dialog in Qt is a file name. It doesn’t create or open a file. You can set name or mime type filters, e.g. to limit or exclude suffixes. You can modify the return value before creating a file, even exchanging one suffix with another.

      Software Engineer
      The Qt Company, Oslo

      MarisaGM 1 Reply Last reply
      4
      • Axel SpoerlA Axel Spoerl

        The return value of a file dialog in Qt is a file name. It doesn’t create or open a file. You can set name or mime type filters, e.g. to limit or exclude suffixes. You can modify the return value before creating a file, even exchanging one suffix with another.

        MarisaGM Offline
        MarisaGM Offline
        MarisaG
        wrote on last edited by MarisaG
        #3

        @Axel-Spoerl ah cool, thanks. I will put in code to check if the filename ends in .emp and add it if not, then save to that file.
        Is there a way to have my filter default to "on" rather than a option? I have a filter that lists files of my configured type, for instance *.emp, but the file browser defaults to showing all files.

        --- https://beos.retro-os.live- support and community for BeOS & Haiku

        1 Reply Last reply
        0
        • JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          If you have specified a filter it should be applied. However, this usually occurs when you are opening (an existing file), when saving a file it's not so important (only relevant to show you what files you might overwrite, but of course unlike when opening you do not have to pick one). Behaviour might depend on whether you are using the native File Browser or the Qt one, and which platform you are on.

          MarisaGM 1 Reply Last reply
          0
          • JonBJ JonB

            If you have specified a filter it should be applied. However, this usually occurs when you are opening (an existing file), when saving a file it's not so important (only relevant to show you what files you might overwrite, but of course unlike when opening you do not have to pick one). Behaviour might depend on whether you are using the native File Browser or the Qt one, and which platform you are on.

            MarisaGM Offline
            MarisaGM Offline
            MarisaG
            wrote on last edited by
            #5

            @JonB I have the filter, and if I manually choose it it gets applied, but I would like the filter to be applied by default and the "show all" filter to be optional. I'm using qt5 on Haiku... And using the qt file dialog.

            --- https://beos.retro-os.live- support and community for BeOS & Haiku

            M 1 Reply Last reply
            0
            • JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              I don't know what "Haiku" is as an operating system. I can only say that under Ubuntu 22.04 with Qt5 (all I have)

              QFileDialog::getSaveFileName(&w, "Caption", QString(), "*.cpp");
              // or
              QFileDialog::getSaveFileName(&w, "Caption", QString(), "*.cpp", nullptr, QFileDialog::DontUseNativeDialog);
              

              These use native dialog/Qt dialog respectively. Both apply the *.cpp filter from the outset, and neither has a "show all" filter.

              MarisaGM 1 Reply Last reply
              0
              • JonBJ JonB

                I don't know what "Haiku" is as an operating system. I can only say that under Ubuntu 22.04 with Qt5 (all I have)

                QFileDialog::getSaveFileName(&w, "Caption", QString(), "*.cpp");
                // or
                QFileDialog::getSaveFileName(&w, "Caption", QString(), "*.cpp", nullptr, QFileDialog::DontUseNativeDialog);
                

                These use native dialog/Qt dialog respectively. Both apply the *.cpp filter from the outset, and neither has a "show all" filter.

                MarisaGM Offline
                MarisaGM Offline
                MarisaG
                wrote on last edited by
                #7

                @JonB let me try a screenshot of what I have or the code I'm using.

                --- https://beos.retro-os.live- support and community for BeOS & Haiku

                1 Reply Last reply
                0
                • MarisaGM Offline
                  MarisaGM Offline
                  MarisaG
                  wrote on last edited by
                  #8

                  if (maybeSave()) {
                  QString fileName = QFileDialog::getOpenFileName(this,
                  tr("Open Empire Config"),"", tr("Empire Config Files (*.emp)"));
                  if (!fileName.isEmpty())
                  loadFile(fileName);
                  }

                  --- https://beos.retro-os.live- support and community for BeOS & Haiku

                  JonBJ 1 Reply Last reply
                  0
                  • MarisaGM MarisaG

                    if (maybeSave()) {
                    QString fileName = QFileDialog::getOpenFileName(this,
                    tr("Open Empire Config"),"", tr("Empire Config Files (*.emp)"));
                    if (!fileName.isEmpty())
                    loadFile(fileName);
                    }

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

                    @MarisaG
                    Yep same as mine. Just try *.emp in place of Empire Config Files (*.emp), though it should be same.

                    I think actually you are using your OS' native dialog, and that may behave differently (and I don't know whether you can affect it). Try my one with QFileDialog::DontUseNativeDialog and see how that compares/behaves.

                    1 Reply Last reply
                    0
                    • MarisaGM MarisaG

                      @JonB I have the filter, and if I manually choose it it gets applied, but I would like the filter to be applied by default and the "show all" filter to be optional. I'm using qt5 on Haiku... And using the qt file dialog.

                      M Offline
                      M Offline
                      mpergand
                      wrote on last edited by
                      #10

                      @MarisaG said in setting default file extension when creating a new file...:

                      I have the filter, and if I manually choose it it gets applied, but I would like the filter to be applied by default and the "show all" filter to be optional.

                      Try:

                      QString fileName = QFileDialog::getOpenFileName(0L,
                        "Open Empire Config","", "Empire Config Files (*.emp) ;; All Files (*)");
                      
                      1 Reply Last reply
                      1
                      • MarisaGM Offline
                        MarisaGM Offline
                        MarisaG
                        wrote on last edited by
                        #11

                        That gives me a second "All Files" fiter:
                        image.png

                        the one at the top is what is already there...

                        --- https://beos.retro-os.live- support and community for BeOS & Haiku

                        M 1 Reply Last reply
                        0
                        • MarisaGM MarisaG

                          That gives me a second "All Files" fiter:
                          image.png

                          the one at the top is what is already there...

                          M Offline
                          M Offline
                          mpergand
                          wrote on last edited by mpergand
                          #12

                          @MarisaG
                          Not the case in Kubuntu 22.04:
                          alt text

                          1 Reply Last reply
                          0
                          • MarisaGM Offline
                            MarisaGM Offline
                            MarisaG
                            wrote on last edited by
                            #13

                            Hmm :( Maybe i will have to ask in the haiku forums for people who have experience with qt...

                            --- https://beos.retro-os.live- support and community for BeOS & Haiku

                            Pl45m4P 1 Reply Last reply
                            0
                            • MarisaGM MarisaG

                              Hmm :( Maybe i will have to ask in the haiku forums for people who have experience with qt...

                              Pl45m4P Offline
                              Pl45m4P Offline
                              Pl45m4
                              wrote on last edited by
                              #14

                              @MarisaG said in setting default file extension when creating a new file...:

                              Hmm :( Maybe i will have to ask in the haiku forums for people who have experience with qt...

                              Could be "Haiku" related behavior.
                              Have you tried @JonB 's suggestion here:

                              @JonB said in setting default file extension when creating a new file...:

                              Try my one with QFileDialog::DontUseNativeDialog and see how that compares/behaves.

                              Maybe Haiku adds "All files" by default or so. Don't know. Usually, on Windows, Linux and even Mac (AFAIK) you don't have "All files", when you set a filter which excludes it.


                              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                              ~E. W. Dijkstra

                              MarisaGM 1 Reply Last reply
                              0
                              • Pl45m4P Pl45m4

                                @MarisaG said in setting default file extension when creating a new file...:

                                Hmm :( Maybe i will have to ask in the haiku forums for people who have experience with qt...

                                Could be "Haiku" related behavior.
                                Have you tried @JonB 's suggestion here:

                                @JonB said in setting default file extension when creating a new file...:

                                Try my one with QFileDialog::DontUseNativeDialog and see how that compares/behaves.

                                Maybe Haiku adds "All files" by default or so. Don't know. Usually, on Windows, Linux and even Mac (AFAIK) you don't have "All files", when you set a filter which excludes it.

                                MarisaGM Offline
                                MarisaGM Offline
                                MarisaG
                                wrote on last edited by MarisaG
                                #15

                                @Pl45m4 I do want it to look like a native app, so i like the way this is working other than the file list. I rearranged my file layout so that it defaults to looking for the config files for empclient under the system config area so this is not as much a issue anymore.

                                I'm sure i will have more questions LOL.

                                --- https://beos.retro-os.live- support and community for BeOS & Haiku

                                Pl45m4P 1 Reply Last reply
                                0
                                • MarisaGM MarisaG

                                  @Pl45m4 I do want it to look like a native app, so i like the way this is working other than the file list. I rearranged my file layout so that it defaults to looking for the config files for empclient under the system config area so this is not as much a issue anymore.

                                  I'm sure i will have more questions LOL.

                                  Pl45m4P Offline
                                  Pl45m4P Offline
                                  Pl45m4
                                  wrote on last edited by Pl45m4
                                  #16

                                  @MarisaG said in setting default file extension when creating a new file...:

                                  I do want it to look like a native app, so i like the way this is working other than the file list

                                  Yes, understood. But have you tried it? :)
                                  If you try Qt's FileDialog (QFileDialog::DontUseNativeDialog flag set) and there is only one "All Files" left, then you know for sure that it is Haiku's default file dialog that has its own "All Files" (which Qt has no control over).


                                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                  ~E. W. Dijkstra

                                  1 Reply Last reply
                                  1

                                  • Login

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