File-save doesn't prepend extension?
-
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?
-
@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); } }
-
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?
@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:
I think what you are looking is called "Filter", to only allow one or some file extentions/type.
-
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. -
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 -
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
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. -
@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.@clarify OK! I apologize. I read your question carelessly. I'll check this out!
-
@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.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!
-
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
Amazing! That solved it.
The dialog looks totally different now, and better.
And there's no longer any drop-down.
Thanks very much. -
-
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?
@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 filesQFileDialog
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 typesabc
the file dialog does not treat this asabc.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 defaultQFileDialog::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.