QFileDialog for "Save As" with choices
-
My code has a generic way of exporting from a Qt model/table/view to a CSV file. I now want to expand that so that the user has a choice between saving/exporting to an XSLX file or a CSV file.
Since the code for exporting to XSLX is different for that to CSV, I need to know which one he wants as well as the filename. I want to keep the number of user interactions to a minimum, so I would like to get the choice of CSV vs XSLX in the same dialog as where the user gets to specify the filename.
How would you choose to implement this?
-
Can I sub-class
QFileDialog
somehow to replace the single the single "Save" button alternative "Save as CSV" & "Save as XSLX" buttons? -
Or, would you think sufficient to rely on having it offer separate "Files of type" choices for
*.csv
and for `*.xslx", and have caller check that filter choices/final filename to make the decision?
When you're in Excel/Word/LO Calc/Write, and others, IIRC the specialised "Save as" file dialog they have invites user to choose from combo which file type format they want exported to. I guess that's the sort of thing I'm thinking of.
Thoughts/advice?
-
-
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 staticgetSaveFileName()
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. -
My code has a generic way of exporting from a Qt model/table/view to a CSV file. I now want to expand that so that the user has a choice between saving/exporting to an XSLX file or a CSV file.
Since the code for exporting to XSLX is different for that to CSV, I need to know which one he wants as well as the filename. I want to keep the number of user interactions to a minimum, so I would like to get the choice of CSV vs XSLX in the same dialog as where the user gets to specify the filename.
How would you choose to implement this?
-
Can I sub-class
QFileDialog
somehow to replace the single the single "Save" button alternative "Save as CSV" & "Save as XSLX" buttons? -
Or, would you think sufficient to rely on having it offer separate "Files of type" choices for
*.csv
and for `*.xslx", and have caller check that filter choices/final filename to make the decision?
When you're in Excel/Word/LO Calc/Write, and others, IIRC the specialised "Save as" file dialog they have invites user to choose from combo which file type format they want exported to. I guess that's the sort of thing I'm thinking of.
Thoughts/advice?
Stupid question: why are you not using http://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName directly ?
When setting the filter like given in the example
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
The user gets the choise and when selected "Text files (*.txt)" (s)he may type simply the base name and ".txt" is appended. Your app can check the extension and output in appropriate format.
-
-
My code has a generic way of exporting from a Qt model/table/view to a CSV file. I now want to expand that so that the user has a choice between saving/exporting to an XSLX file or a CSV file.
Since the code for exporting to XSLX is different for that to CSV, I need to know which one he wants as well as the filename. I want to keep the number of user interactions to a minimum, so I would like to get the choice of CSV vs XSLX in the same dialog as where the user gets to specify the filename.
How would you choose to implement this?
-
Can I sub-class
QFileDialog
somehow to replace the single the single "Save" button alternative "Save as CSV" & "Save as XSLX" buttons? -
Or, would you think sufficient to rely on having it offer separate "Files of type" choices for
*.csv
and for `*.xslx", and have caller check that filter choices/final filename to make the decision?
When you're in Excel/Word/LO Calc/Write, and others, IIRC the specialised "Save as" file dialog they have invites user to choose from combo which file type format they want exported to. I guess that's the sort of thing I'm thinking of.
Thoughts/advice?
-
-
My code has a generic way of exporting from a Qt model/table/view to a CSV file. I now want to expand that so that the user has a choice between saving/exporting to an XSLX file or a CSV file.
Since the code for exporting to XSLX is different for that to CSV, I need to know which one he wants as well as the filename. I want to keep the number of user interactions to a minimum, so I would like to get the choice of CSV vs XSLX in the same dialog as where the user gets to specify the filename.
How would you choose to implement this?
-
Can I sub-class
QFileDialog
somehow to replace the single the single "Save" button alternative "Save as CSV" & "Save as XSLX" buttons? -
Or, would you think sufficient to rely on having it offer separate "Files of type" choices for
*.csv
and for `*.xslx", and have caller check that filter choices/final filename to make the decision?
When you're in Excel/Word/LO Calc/Write, and others, IIRC the specialised "Save as" file dialog they have invites user to choose from combo which file type format they want exported to. I guess that's the sort of thing I'm thinking of.
Thoughts/advice?
-
-
Stupid question: why are you not using http://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName directly ?
When setting the filter like given in the example
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
The user gets the choise and when selected "Text files (*.txt)" (s)he may type simply the base name and ".txt" is appended. Your app can check the extension and output in appropriate format.
@koahnig , @CP71
I would be happy to use the standardgetSaveFileName()
provided I can get what I want out of it.So your proposal is: forget my first possibility of trying to add a button. Do it via the user chooses the "Files of type" combo.
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
- But does that generate one choice or does that offer three separate choices to the user? [Sorry I cannot test ATM.]
- And assuming it does, I'd really have liked something which read something like
Save as CSV file (*.csv)
, does it allow that? - Finally, can't the user still type in
filename.anything
? It worries me just checking the extension...
I worry that [my] users won't understand they need to go click
Files of type
to indicate what they want to export to? -
@koahnig , @CP71
I would be happy to use the standardgetSaveFileName()
provided I can get what I want out of it.So your proposal is: forget my first possibility of trying to add a button. Do it via the user chooses the "Files of type" combo.
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
- But does that generate one choice or does that offer three separate choices to the user? [Sorry I cannot test ATM.]
- And assuming it does, I'd really have liked something which read something like
Save as CSV file (*.csv)
, does it allow that? - Finally, can't the user still type in
filename.anything
? It worries me just checking the extension...
I worry that [my] users won't understand they need to go click
Files of type
to indicate what they want to export to?I worry that [my] users won't understand they need to go click Files of type to indicate what they want to export to?
I'd say that is pretty standard behavior. File > Save as... > Select file type from combo box > Enter file name > Save.
Regards
-
I worry that [my] users won't understand they need to go click Files of type to indicate what they want to export to?
I'd say that is pretty standard behavior. File > Save as... > Select file type from combo box > Enter file name > Save.
Regards
-
You get these choises
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>
#------------------------------------------------- # # 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
-
You get these choises
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>
#------------------------------------------------- # # 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
@koahnig
In your (German) screen shot you have labelDateitype
against the dropdown for the choices. We getFiles of type
. Is it possible to alter that text, so that I could make it read something likeExport/Save to
? That is what would help me help my users. Trouble is, I suspect the answer is "no"... -
@koahnig
In your (German) screen shot you have labelDateitype
against the dropdown for the choices. We getFiles of type
. Is it possible to alter that text, so that I could make it read something likeExport/Save to
? That is what would help me help my users. Trouble is, I suspect the answer is "no"...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.
-
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.
@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...?
-
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.
-
@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...?
-
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.
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.
-
@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.
-
@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.
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).
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.
-
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).
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.
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 :)
-
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 staticgetSaveFileName()
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.