Solved QFileDialog doesn't add selected extension
-
I'm trying to use QFileDialog to create a Save As popup.
I can enter the filename e.g. "foo", but if I selected the "PNG (.png)" file type, the dialog doesn't add that extension for me.
Is there a way to make it do that?
Thanks. -
@Publicnamer
Does not show the extension-type in the filter selector? Does not append that extension in the returned file path? Or what? -
@Publicnamer can you show your code (QFileDialog to create a Save As popup)?
-
It shows the available file types and extensions in the pull-down menu, but it does not append the extension.
const QStringList filters({ "Thingy (*.thi)", "Dingy (*.dni)" }); QFileDialog fileDialog(this); fileDialog.setAcceptMode(QFileDialog::AcceptSave); fileDialog.setNameFilters(filters); fileDialog.exec(); if (!fileDialog.selectedFiles().count()) return; QString filename = fileDialog.selectedFiles().first();
I don't know why it isn't automatically appending the extension and I'm unsure how I can learn which extension the user selected.
-
@Publicnamer
Use this for current selected extensionQString extension = fileDialog.selectedNameFilter();
Details SelectedNameFilter
-
This works as expected for me (Linux, Qt 5.15):
#include <QApplication> #include <QFileDialog> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); const QStringList filters({ "Thingy (*.thi)", "Dingy (*.dni)" }); QFileDialog fileDialog; fileDialog.setDirectory("/tmp/testfolder"); fileDialog.setAcceptMode(QFileDialog::AcceptSave); fileDialog.setNameFilters(filters); fileDialog.exec(); if (fileDialog.selectedFiles().count()) qDebug() << fileDialog.selectedFiles(); return 0; }
Run it, select Thingy file type, enter
fred
in the file name and this is the result:17:20:46: Starting /tmp/build-untitled-Desktop-Debug/untitled ... ("/tmp/testfolder/fred.thi") 17:20:49: /tmp/build-untitled-Desktop-Debug/untitled exited with code 0
or for Dingy:
17:20:53: Starting /tmp/build-untitled-Desktop-Debug/untitled ... ("/tmp/testfolder/fred.dni") 17:21:04: /tmp/build-untitled-Desktop-Debug/untitled exited with code 0
-
@ChrisW67 said in QFileDialog doesn't add selected extension:
This works as expected for me
That's what I thought ought be the case, because I thought somewhere the
QFileDialog
docs said it returns a full path to the selected filename....Since the OP does not bother to mention what platform he is on we usually have to guess that means Windows. Maybe that uses the native Windows dialog and returns a different result?
-
I went with Linux because many of the Op's other post refer to PinePhone, which runs Linux.
I've used the dialog on Windows in the past without the issue described. Don't have a Windows dev environment to hand at the moment to test it with exactly the same Qt version.
-
I experienced similar issue with earlier version of Qt5 with linux
if(fileDialog.exec()) { QStringList list=fileDialog.selectedFiles(); prefs.setValue(Prefs_SaveDialogLastPath,fileDialog.directoryUrl()); if(!list.isEmpty()) { filename=list.at(0); fileInfo.setFile(filename); QString ext=fileInfo.suffix(); if(ext.isEmpty()) // ajoute l'extension sélectionnée (pas automatique sous linux) { ext=fileDialog.selectedNameFilter(); int start=ext.lastIndexOf('.'); if(start>0) { ext=ext.mid(start,ext.length()-start-1); if(!ext.isEmpty()) filename+=ext; } } } }
Since i maneged this issue by myself, i don't know if it has been fixed since then.
-
@anil_arise That returns the full extension string. I'd have to parse it to extract the .thi or .dni.
I wonder why Qt doesn't offer just the extension. -
@ChrisW67 I'm using Linux. I'm using Qt 5.11.3 for aarch64.
I find that if I add only one name filter then when I type a single letter e.g. "a", a drop-down appears that says "a.thi". If I ignore that and click Save, then dialog will end up giving me just /home/me/a and not /home/me/a.thi.
I don't recall seeing that drop-down when I had more than one name filter.I can just manually add the extension after I get the first selectedFile, but if a file by the name a.thi already exists, the problem is the user will never see the fileDialog's usual "Really replace this file" type of pop-up.
Thus I think this is just a Qt bug.
-
@mpergand said in QFileDialog doesn't add selected extension:
QString ext=fileInfo.suffix();
You will find it difficult to tell
.tar.gz
from.gz
in example. I'd suggest usingQFileInfo::completeSuffix()
instead. -
@mpergand said in QFileDialog doesn't add selected extension:
ajoute l'extension sélectionnée (pas automatique sous linux)
To summarize then my question is why isn't it automatic under Linux.
I suppose I could search through the Qt source code to find out but I gather Qt has people on staff to fix bugs. -
@Publicnamer said in QFileDialog doesn't add selected extension:
but I gather Qt has people on staff to fix bugs
Yes, but not in this user forum.
If you think there is a bug please file a bug in https://bugreports.qt.io/secure/Dashboard.jspa -
@Publicnamer
QFileDialog
can appear in two different forms, a Qt dialog or a native dialog. See https://doc.qt.io/qt-5/qfiledialog.html#Option-enumQFileDialog::DontUseNativeDialog
. Different platforms may use the Qt or the native version regardless in certain situations.I think one thing you should look at is whether in your case on your platform it is using the Qt or native dialog, and report back here for others to compare. Also if you are able to try with the opposite setting for
QFileDialog::DontUseNativeDialog
, if that produces the other version does that still exhibit the failure to append the extension? Behaviour may not differ, I don't know, but it could be relevant. -
@Publicnamer
Furthermore, you are using code to create aQFileDialog
instance and using that. Which is fine. However there is also astatic
method for saving a file, https://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName. I think you should also test that for your situation and see whether the result is any different. -
@JonB I just saw your message. Yes I've found that QFileDialog::getSaveFileName does not exhibit the bug, so I've switched to that.
-
@Publicnamer
Interesting. I'm glad it has worked fro you, but puzzled. The code can be seen in https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qfiledialog.cpp.html#_ZN11QFileDialog15getSaveFileNameEP7QWidgetRK7QStringS4_S4_PS2_6QFlagsINS_6OptionEE . That just calls https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qfiledialog.cpp.html#_ZN11QFileDialog14getSaveFileUrlEP7QWidgetRK7QStringRK4QUrlS4_PS2_6QFlagsINS_6OptionEERK11QStringList . That effectively just goesif (dialog.exec() == QDialog::Accepted) { if (selectedFilter) *selectedFilter = dialog.selectedNameFilter(); return dialog.selectedUrls().value(0); }
Quite why that would append an extension to the returned file/URL, where your own original code did not, I do not know.