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.
  • M Offline
    M Offline
    MarisaG
    wrote on 14 May 2024, 05:30 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
    • A Online
      A Online
      Axel Spoerl
      Moderators
      wrote on 14 May 2024, 05:41 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

      M 1 Reply Last reply 14 May 2024, 08:33
      4
      • A Axel Spoerl
        14 May 2024, 05:41

        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.

        M Offline
        M Offline
        MarisaG
        wrote on 14 May 2024, 08:33 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
        • J Offline
          J Offline
          JonB
          wrote on 14 May 2024, 08:39 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.

          M 1 Reply Last reply 14 May 2024, 08:55
          0
          • J JonB
            14 May 2024, 08:39

            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.

            M Offline
            M Offline
            MarisaG
            wrote on 14 May 2024, 08:55 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 14 May 2024, 11:21
            0
            • J Offline
              J Offline
              JonB
              wrote on 14 May 2024, 09:27 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.

              M 1 Reply Last reply 14 May 2024, 09:59
              0
              • J JonB
                14 May 2024, 09:27

                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.

                M Offline
                M Offline
                MarisaG
                wrote on 14 May 2024, 09:59 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
                • M Offline
                  M Offline
                  MarisaG
                  wrote on 14 May 2024, 11:00 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

                  J 1 Reply Last reply 14 May 2024, 11:13
                  0
                  • M MarisaG
                    14 May 2024, 11:00

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 14 May 2024, 11:13 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
                    • M MarisaG
                      14 May 2024, 08:55

                      @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 14 May 2024, 11:21 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
                      • M Offline
                        M Offline
                        MarisaG
                        wrote on 14 May 2024, 11:30 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 14 May 2024, 11:49
                        0
                        • M MarisaG
                          14 May 2024, 11:30

                          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 14 May 2024, 11:49 last edited by mpergand
                          #12

                          @MarisaG
                          Not the case in Kubuntu 22.04:
                          alt text

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            MarisaG
                            wrote on 14 May 2024, 11:56 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

                            P 1 Reply Last reply 14 May 2024, 12:30
                            0
                            • M MarisaG
                              14 May 2024, 11:56

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

                              P Offline
                              P Offline
                              Pl45m4
                              wrote on 14 May 2024, 12:30 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

                              M 1 Reply Last reply 14 May 2024, 13:53
                              0
                              • P Pl45m4
                                14 May 2024, 12:30

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

                                M Offline
                                M Offline
                                MarisaG
                                wrote on 14 May 2024, 13:53 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

                                P 1 Reply Last reply 14 May 2024, 14:00
                                0
                                • M MarisaG
                                  14 May 2024, 13:53

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

                                  P Offline
                                  P Offline
                                  Pl45m4
                                  wrote on 14 May 2024, 14:00 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

                                  8/16

                                  14 May 2024, 11:00

                                  • Login

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