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. File-save doesn't prepend extension?
Forum Updated to NodeBB v4.3 + New Features

File-save doesn't prepend extension?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 640 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.
  • C clarify

    In many operating systems, when the user does a File Save operation and they have to choose the filename, the extension is prepended and cannot be altered/deleted.

    E.g. if my code tells the File Save dialog to save as a ".txt" file, then the user sees the .txt to the right of the cursor where he has to type the file name and he cannot delete the .txt.

    Does Qt's file save have an option to support that behavior?

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

    @clarify
    [You mean "append" extension rather than "prepend".]
    In a word, no, there is no facility for this.
    I don't know whether someone has tried subclassing the dialog and implementing it, but I haven't come across it.

    1 Reply Last reply
    0
    • posktomtenP Offline
      posktomtenP Offline
      posktomten
      wrote on last edited by
      #3

      @clarify Hello!

      I think

      dialog->setDefaultSuffix(".txt");

      fix that thing.

      #include "mainwindow.h"
      #include "./ui_mainwindow.h"
      #include <QFileDialog>
      #include <QFileInfo>
      #include <QMessageBox>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          QFileDialog *dialog = new QFileDialog(this);
          dialog->setLabelText(QFileDialog::Accept, tr("Save"));
          dialog->setLabelText(QFileDialog::Reject, tr("Cancel"));
          dialog->setWindowTitle(tr("Save as text file"));
          dialog->setViewMode(QFileDialog::Detail);
          dialog->setFileMode(QFileDialog::AnyFile);
          dialog->setAcceptMode(QFileDialog::AcceptSave);
          QStringList filters;
          filters << tr("Text file") + " (*.txt)" << tr("Text file, optional extension") + " (*)";
          dialog->setNameFilters(filters);
      
          if(dialog->exec() == 1) {
      #if defined(Q_OS_LINUX)
      
              if(dialog->selectedNameFilter() == tr("Text file") + " (*.txt)") {
                  dialog->setDefaultSuffix(".txt");
              }
      
      #endif
              QString newfile = dialog->selectedFiles().at(0);
              QFileInfo fi(newfile);
              QString path = fi.absolutePath();
              QFileInfo fi1(path);
      
              if(!fi1.isWritable()) {
                  QMessageBox msgBox;
                  msgBox.setIcon(QMessageBox::Critical);
                  msgBox.setText(tr("Could not save the file.\nCheck your file permissions."));
                  msgBox.addButton(tr("Ok"), QMessageBox::AcceptRole);
                  msgBox.exec();
                  return;
              }
      
              QFile file_newfile(newfile);
              QString text = "Text to be saved to a file";
              file_newfile.open(QIODevice::WriteOnly | QIODevice::Text);
              QTextStream outStream(&file_newfile);
              outStream << text;
              file_newfile.close();
              fi.setFile(file_newfile);
          }
      }
      

      posktomten

      1 Reply Last reply
      0
      • C clarify

        In many operating systems, when the user does a File Save operation and they have to choose the filename, the extension is prepended and cannot be altered/deleted.

        E.g. if my code tells the File Save dialog to save as a ".txt" file, then the user sees the .txt to the right of the cursor where he has to type the file name and he cannot delete the .txt.

        Does Qt's file save have an option to support that behavior?

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

        @clarify said in File-save doesn't prepend extension?:

        Does Qt's file save have an option to support that behavior?

        Have a look here:

        • https://doc.qt.io/qt-6/qfiledialog.html#getSaveFileName

        I think what you are looking is called "Filter", to only allow one or some file extentions/type.


        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
        0
        • C Offline
          C Offline
          clarify
          wrote on last edited by
          #5

          This has nearly the same effect as when I was using the convenience function QFileDialog::getSaveFileName.

          Currently I still have a problem, besides the missing suffix.

          Let's say there already exists a file asdfasdf.html.
          But I'm saving as asdfasdf.txt.
          When I type asdfasdf in the dialog, it will still have a dropdown that shows asdfasdf.html.
          Do you know how I can disable that dropdown?
          Thanks.

          1 Reply Last reply
          0
          • posktomtenP Offline
            posktomtenP Offline
            posktomten
            wrote on last edited by
            #6
            const QStringList filters({"Image files (*.png *.xpm *.jpg)",
                                       "Text files (*.txt)",
                                       "Any files (*)"
                                      });
            QFileDialog dialog(this);
            dialog.setNameFilters(filters);
            dialog.exec();
            

            Only files with file extensions included in filters are visible.
            https://doc.qt.io/qt-6/qfiledialog.html#setNameFilters

            posktomten

            C 1 Reply Last reply
            0
            • posktomtenP posktomten
              const QStringList filters({"Image files (*.png *.xpm *.jpg)",
                                         "Text files (*.txt)",
                                         "Any files (*)"
                                        });
              QFileDialog dialog(this);
              dialog.setNameFilters(filters);
              dialog.exec();
              

              Only files with file extensions included in filters are visible.
              https://doc.qt.io/qt-6/qfiledialog.html#setNameFilters

              C Offline
              C Offline
              clarify
              wrote on last edited by
              #7

              @posktomten
              Visible where, though?
              There's the list of files, that is filtered.
              But in the drop-down below the text field, there is another list that ignores the filter.
              Maybe it's a bug? I'm using Qt 5.

              posktomtenP 2 Replies Last reply
              0
              • C clarify

                @posktomten
                Visible where, though?
                There's the list of files, that is filtered.
                But in the drop-down below the text field, there is another list that ignores the filter.
                Maybe it's a bug? I'm using Qt 5.

                posktomtenP Offline
                posktomtenP Offline
                posktomten
                wrote on last edited by
                #8

                @clarify OK! I apologize. I read your question carelessly. I'll check this out!

                posktomten

                1 Reply Last reply
                0
                • C clarify

                  @posktomten
                  Visible where, though?
                  There's the list of files, that is filtered.
                  But in the drop-down below the text field, there is another list that ignores the filter.
                  Maybe it's a bug? I'm using Qt 5.

                  posktomtenP Offline
                  posktomtenP Offline
                  posktomten
                  wrote on last edited by
                  #9

                  @clarify

                  QFileDialog dialog(this);
                      dialog.setOption(QFileDialog::DontUseNativeDialog);
                      dialog.setFileMode(QFileDialog::AnyFile);
                      dialog.setNameFilter(tr("text file (*.txt)"));
                      QStringList fileNames;
                  
                      if(dialog.exec())
                          fileNames = dialog.selectedFiles();
                  
                  

                  Windows 10
                  If I use Windows' own file dialogs, I can't get it to work the way you want it to. Probably won't work, it's up to Windows to decide. If you use Qt's own file dialog, it turned out the way you want it. But it looks a little different then.

                  dialog.setOption(QFileDialog::DontUseNativeDialog)
                  

                  Hope you got some help moving on!

                  posktomten

                  C 1 Reply Last reply
                  0
                  • posktomtenP posktomten

                    @clarify

                    QFileDialog dialog(this);
                        dialog.setOption(QFileDialog::DontUseNativeDialog);
                        dialog.setFileMode(QFileDialog::AnyFile);
                        dialog.setNameFilter(tr("text file (*.txt)"));
                        QStringList fileNames;
                    
                        if(dialog.exec())
                            fileNames = dialog.selectedFiles();
                    
                    

                    Windows 10
                    If I use Windows' own file dialogs, I can't get it to work the way you want it to. Probably won't work, it's up to Windows to decide. If you use Qt's own file dialog, it turned out the way you want it. But it looks a little different then.

                    dialog.setOption(QFileDialog::DontUseNativeDialog)
                    

                    Hope you got some help moving on!

                    C Offline
                    C Offline
                    clarify
                    wrote on last edited by
                    #10

                    @posktomten
                    Amazing! That solved it.
                    The dialog looks totally different now, and better.
                    And there's no longer any drop-down.
                    Thanks very much.

                    1 Reply Last reply
                    0
                    • C clarify has marked this topic as solved on
                    • C clarify

                      In many operating systems, when the user does a File Save operation and they have to choose the filename, the extension is prepended and cannot be altered/deleted.

                      E.g. if my code tells the File Save dialog to save as a ".txt" file, then the user sees the .txt to the right of the cursor where he has to type the file name and he cannot delete the .txt.

                      Does Qt's file save have an option to support that behavior?

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

                      @clarify said in File-save doesn't prepend extension?:

                      E.g. if my code tells the File Save dialog to save as a ".txt" file, then the user sees the .txt to the right of the cursor where he has to type the file name and he cannot delete the .txt.

                      I really don't see how your accepted solution satisfies your stated requirement. Which is why I said Qt does not offer this.

                      QDialog::setNameFiler() does nothing other than control what existing files QFileDialog displays. It does not even have any effect on the string(s) returned, which are just the current path in the viewport. Which may or may not have a .txt extension. You have to deal with that yourself in code.

                      Also, the solution you have now is a file dialog for opening a file with the intention of reading. You should add dialog.setAcceptMode(QFileDialog::AcceptSave) for your intended "File Save operation". This will (a) change the title of the dialog (from "Open" to "Save As") and (b) change behaviour if an existing file name is selected or typed in (so that it asks about overwriting, as a user would expect).

                      It's still not great as if there is a file named abc.txt and rather than selecting it the user types abc the file dialog does not treat this as abc.txt for checking, even though your code is likely to be appending the .txt according to your rule. But it's still better than leaving it on the default QFileDialog::AcceptOpen.

                      So far as I can see, your requirement is reasonable, and may be implemented in other applications, but is not well supported in Qt.

                      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