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 for "Save As" with choices

QFileDialog for "Save As" with choices

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 14.1k 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.
  • J JonB
    25 Jan 2019, 19:49

    @aha_1980 OK I'll give it a go and see what it looks like when I get back to it.

    K Offline
    K Offline
    koahnig
    wrote on 26 Jan 2019, 10:46 last edited by
    #8

    @JonB

    You get these choises
    0_1548499335505_f8c33aa1-0908-4af9-ae2e-a4624bf20b15-image.png

    the tset app is based on a creator template
    main.cpp

    #include "MainWindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    MainWindow.cpp

    #include "MainWindow.h"
    #include "ui_MainWindow.h"
    
    #include <QFileDialog>
    #include <QString>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::on_actiontestSave_triggered()
    {
        saveFile();
    }
    void MainWindow::saveFile ()
    {
        QString fname = QFileDialog::getSaveFileName(nullptr, "test sav e name", ".", "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" );
        qDebug() << "name is : " << fname;
    }
    

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    public slots:
        void on_actiontestSave_triggered();
        void saveFile ();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    MainWindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>400</width>
        <height>300</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget"/>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>400</width>
         <height>21</height>
        </rect>
       </property>
       <widget class="QMenu" name="menuFile">
        <property name="title">
         <string>File</string>
        </property>
        <addaction name="actiontestSave"/>
       </widget>
       <addaction name="menuFile"/>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
      <action name="actiontestSave">
       <property name="text">
        <string>testSave</string>
       </property>
      </action>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    

    TestFileDialog.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2019-01-25T18:08:57
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = TestFileDialog
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    CONFIG += c++11
    
    SOURCES += \
            main.cpp \
            MainWindow.cpp
    
    HEADERS += \
            MainWindow.h
    
    FORMS += \
            MainWindow.ui
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    Vote the answer(s) that helped you to solve your issue(s)

    J 1 Reply Last reply 26 Jan 2019, 11:03
    2
    • K koahnig
      26 Jan 2019, 10:46

      @JonB

      You get these choises
      0_1548499335505_f8c33aa1-0908-4af9-ae2e-a4624bf20b15-image.png

      the tset app is based on a creator template
      main.cpp

      #include "MainWindow.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      
          return a.exec();
      }
      

      MainWindow.cpp

      #include "MainWindow.h"
      #include "ui_MainWindow.h"
      
      #include <QFileDialog>
      #include <QString>
      #include <QDebug>
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      void MainWindow::on_actiontestSave_triggered()
      {
          saveFile();
      }
      void MainWindow::saveFile ()
      {
          QString fname = QFileDialog::getSaveFileName(nullptr, "test sav e name", ".", "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" );
          qDebug() << "name is : " << fname;
      }
      

      MainWindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      public slots:
          void on_actiontestSave_triggered();
          void saveFile ();
      
      private:
          Ui::MainWindow *ui;
      };
      
      #endif // MAINWINDOW_H
      

      MainWindow.ui

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>400</width>
          <height>300</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <widget class="QWidget" name="centralWidget"/>
        <widget class="QMenuBar" name="menuBar">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>0</y>
           <width>400</width>
           <height>21</height>
          </rect>
         </property>
         <widget class="QMenu" name="menuFile">
          <property name="title">
           <string>File</string>
          </property>
          <addaction name="actiontestSave"/>
         </widget>
         <addaction name="menuFile"/>
        </widget>
        <widget class="QToolBar" name="mainToolBar">
         <attribute name="toolBarArea">
          <enum>TopToolBarArea</enum>
         </attribute>
         <attribute name="toolBarBreak">
          <bool>false</bool>
         </attribute>
        </widget>
        <widget class="QStatusBar" name="statusBar"/>
        <action name="actiontestSave">
         <property name="text">
          <string>testSave</string>
         </property>
        </action>
       </widget>
       <layoutdefault spacing="6" margin="11"/>
       <resources/>
       <connections/>
      </ui>
      

      TestFileDialog.pro

      #-------------------------------------------------
      #
      # Project created by QtCreator 2019-01-25T18:08:57
      #
      #-------------------------------------------------
      
      QT       += core gui
      
      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
      
      TARGET = TestFileDialog
      TEMPLATE = app
      
      # The following define makes your compiler emit warnings if you use
      # any feature of Qt which has been marked as deprecated (the exact warnings
      # depend on your compiler). Please consult the documentation of the
      # deprecated API in order to know how to port your code away from it.
      DEFINES += QT_DEPRECATED_WARNINGS
      
      # You can also make your code fail to compile if you use deprecated APIs.
      # In order to do so, uncomment the following line.
      # You can also select to disable deprecated APIs only up to a certain version of Qt.
      #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
      
      CONFIG += c++11
      
      SOURCES += \
              main.cpp \
              MainWindow.cpp
      
      HEADERS += \
              MainWindow.h
      
      FORMS += \
              MainWindow.ui
      
      # Default rules for deployment.
      qnx: target.path = /tmp/$${TARGET}/bin
      else: unix:!android: target.path = /opt/$${TARGET}/bin
      !isEmpty(target.path): INSTALLS += target
      
      J Offline
      J Offline
      JonB
      wrote on 26 Jan 2019, 11:03 last edited by
      #9

      @koahnig
      In your (German) screen shot you have label Dateitype against the dropdown for the choices. We get Files of type. Is it possible to alter that text, so that I could make it read something like Export/Save to? That is what would help me help my users. Trouble is, I suspect the answer is "no"...

      K 1 Reply Last reply 26 Jan 2019, 11:59
      0
      • J JonB
        26 Jan 2019, 11:03

        @koahnig
        In your (German) screen shot you have label Dateitype against the dropdown for the choices. We get Files of type. Is it possible to alter that text, so that I could make it read something like Export/Save to? That is what would help me help my users. Trouble is, I suspect the answer is "no"...

        K Offline
        K Offline
        koahnig
        wrote on 26 Jan 2019, 11:59 last edited by
        #10

        @JonB

        Sorry for German.
        AFAIK is that the standard file dialog as provided by Windows, the so-called native dialog as it is used by Qt routines. I never went through the effort to get the non-native version,which should be possible.

        I doubt that a renaming is possible or easy, since it is the standard dialog of the OS.

        On the other side the standard dialog has a couple of advatnages. The user shall be used to it, because it is the same for all applications oin that computer. Also the language is dependent on the OS language setting as you above.

        When the dialog is ok for you in principle, I am wondering why you want to change it. The further away from standard dialogs you are the more users might be confused. Therefore, personally I think the standard dialogs are more suitable than any variation.

        Vote the answer(s) that helped you to solve your issue(s)

        J 1 Reply Last reply 26 Jan 2019, 12:01
        1
        • K koahnig
          26 Jan 2019, 11:59

          @JonB

          Sorry for German.
          AFAIK is that the standard file dialog as provided by Windows, the so-called native dialog as it is used by Qt routines. I never went through the effort to get the non-native version,which should be possible.

          I doubt that a renaming is possible or easy, since it is the standard dialog of the OS.

          On the other side the standard dialog has a couple of advatnages. The user shall be used to it, because it is the same for all applications oin that computer. Also the language is dependent on the OS language setting as you above.

          When the dialog is ok for you in principle, I am wondering why you want to change it. The further away from standard dialogs you are the more users might be confused. Therefore, personally I think the standard dialogs are more suitable than any variation.

          J Offline
          J Offline
          JonB
          wrote on 26 Jan 2019, 12:01 last edited by JonB
          #11

          @koahnig
          I agree with you & @aha_1980 & others, in that I will try it out with the standard dialog next week. My reservation is that for my users, who are ahem not the greatest, I can see that they would look at a label reading "Choose what format to export" but will simply ignore "Files of type" stating that they have no idea what it means...! They are used to having explicit, distinct buttons: "Export to CSV", "Export to XSLX", etc.

          I doubt that a renaming is possible or easy, since it is the standard dialog of the OS.

          Indeed I suspect so too. If I switched on the "DontUseNativeDialog" I don't suppose I can then change the text...?

          K 1 Reply Last reply 26 Jan 2019, 12:14
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 26 Jan 2019, 12:10 last edited by
            #12

            Hi

            • They are used to having explicit, distinct buttons: "Export to CSV", "Export to XSLX", etc.

            So why do you want to change that ? Just wondering.

            J 1 Reply Last reply 26 Jan 2019, 12:21
            0
            • J JonB
              26 Jan 2019, 12:01

              @koahnig
              I agree with you & @aha_1980 & others, in that I will try it out with the standard dialog next week. My reservation is that for my users, who are ahem not the greatest, I can see that they would look at a label reading "Choose what format to export" but will simply ignore "Files of type" stating that they have no idea what it means...! They are used to having explicit, distinct buttons: "Export to CSV", "Export to XSLX", etc.

              I doubt that a renaming is possible or easy, since it is the standard dialog of the OS.

              Indeed I suspect so too. If I switched on the "DontUseNativeDialog" I don't suppose I can then change the text...?

              K Offline
              K Offline
              koahnig
              wrote on 26 Jan 2019, 12:14 last edited by
              #13

              @JonB

              You are free to change the text of the filter

              0_1548504816877_4941f1c9-9a81-4061-9bf2-fd2c2c6c9ede-image.png

              Vote the answer(s) that helped you to solve your issue(s)

              J 1 Reply Last reply 26 Jan 2019, 12:24
              3
              • M mrjj
                26 Jan 2019, 12:10

                Hi

                • They are used to having explicit, distinct buttons: "Export to CSV", "Export to XSLX", etc.

                So why do you want to change that ? Just wondering.

                J Offline
                J Offline
                JonB
                wrote on 26 Jan 2019, 12:21 last edited by JonB
                #14

                @mrjj

                So why do you want to change that ? Just wondering.

                I don't! That's why I was asking whether it was possible to alter the standard Save dialog to add buttons to it, but as I suspected it does not look like it is. What I (personally) don't like is the two clicks/redraws involved in (a) clicking what to save as followed by (b) the presentation of the dialog to choose filename. I prefer to go into a dialog where I choose the filename and then from the same place choose what to save as, e.g. like I see in MS/Libre Office. That's the whole point of my query. As I have said, I appreciate I'm best using the inbuilt Save As dialog, so have to accept user indicates which format to export in via label reading "Files of type" even though I have reservations, so will try that.

                1 Reply Last reply
                0
                • K koahnig
                  26 Jan 2019, 12:14

                  @JonB

                  You are free to change the text of the filter

                  0_1548504816877_4941f1c9-9a81-4061-9bf2-fd2c2c6c9ede-image.png

                  J Offline
                  J Offline
                  JonB
                  wrote on 26 Jan 2019, 12:24 last edited by JonB
                  #15

                  @koahnig
                  Yes indeed, I have asked about this above and that is what I will be doing. The text like you have or with "Export" will be great. The only shame for me is that I cannot alter "Dateityp" to read what I choose, to get "challenged" users to go to the dropdown in the first place!

                  Actually, now that I see what it looks like, I realise that before it is dropped down the combo will show its current "Save to CSV file" selection (yes, I know I can set default from code, that's great). That will help: when the user sees it initially reading that, it will hopefully encourage him to try the combo if he has in mind a different export type.

                  A 1 Reply Last reply 26 Jan 2019, 12:30
                  0
                  • J JonB
                    26 Jan 2019, 12:24

                    @koahnig
                    Yes indeed, I have asked about this above and that is what I will be doing. The text like you have or with "Export" will be great. The only shame for me is that I cannot alter "Dateityp" to read what I choose, to get "challenged" users to go to the dropdown in the first place!

                    Actually, now that I see what it looks like, I realise that before it is dropped down the combo will show its current "Save to CSV file" selection (yes, I know I can set default from code, that's great). That will help: when the user sees it initially reading that, it will hopefully encourage him to try the combo if he has in mind a different export type.

                    A Offline
                    A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on 26 Jan 2019, 12:30 last edited by
                    #16

                    @JonB

                    I hope you appreciate that I started a Windows VM for you :)

                    This is how the Windows 7 Paint Save As dialog looks like (yeah, German again).

                    0_1548505688398_paing-file-dialog.png

                    It has been like that for over 20 years, and even MS Office / Open Office work exactly like that.

                    That's why I said yesterday, I'd just use the dialog like this - I'm used to that behavior. Your users should too - if they use Windows or Linux. I can't tell for Mac, though.

                    Qt has to stay free or it will die.

                    J 1 Reply Last reply 26 Jan 2019, 12:33
                    1
                    • A aha_1980
                      26 Jan 2019, 12:30

                      @JonB

                      I hope you appreciate that I started a Windows VM for you :)

                      This is how the Windows 7 Paint Save As dialog looks like (yeah, German again).

                      0_1548505688398_paing-file-dialog.png

                      It has been like that for over 20 years, and even MS Office / Open Office work exactly like that.

                      That's why I said yesterday, I'd just use the dialog like this - I'm used to that behavior. Your users should too - if they use Windows or Linux. I can't tell for Mac, though.

                      J Offline
                      J Offline
                      JonB
                      wrote on 26 Jan 2019, 12:33 last edited by JonB
                      #17

                      @aha_1980

                      I hope you appreciate that I started a Windows VM for you :)

                      Indeed I do.

                      It has been like that for over 20 years, and even MS Office / Open Office work exactly like that.

                      Well, no, I was looking at them yesterday and I don't think they do. In particular/at minimum I'm expecting them to have a label different from "Dateityp". Obviously I will now have to go check next week, I don't have either of them on my personal, weekend PC...

                      I'm used to that behavior. Your users should too - if they use Windows or Linux. I can't tell for Mac, though.

                      That's fine. I wouldn't care/program if I had any Mac users :)

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JonB
                        wrote on 29 Jan 2019, 15:00 last edited by JonB
                        #18

                        First thanks to all who commented.

                        I have now had a chance to see what the dialog looks like. I am under Linux. Apart from the fact that the dialog looks rather different, I have had to give up on the whole principle of using QFileDialog::getSaveFileName(). This is because (under Linux) it cannot handle correctly appending a file suffix/extension for the selected file type (as it can under Windows/MacOS), and IMO this makes that dialog "unusable" for my users. This is all covered in e.g. the comments to https://stackoverflow.com/a/1953781/489865. Since I cannot be bothered to give up on the convenient static getSaveFileName() and start writing my own sub-class and do it all via an instance, it works for my current situation to just present my own dialog asking which format to export to and I can get away without asking the user for a path. Ho hum.

                        1 Reply Last reply
                        0

                        17/18

                        26 Jan 2019, 12:33

                        • Login

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