QFileDialog doesn't add selected extension
-
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.
-
@Publicnamer
Use this for current selected extensionQString extension = fileDialog.selectedNameFilter();
Details SelectedNameFilter
@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. -
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.
@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.
-
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.
-
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.
@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. -
@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 -
@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
QFileDialogcan 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. -
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
Furthermore, you are using code to create aQFileDialoginstance and using that. Which is fine. However there is also astaticmethod 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. -
@Publicnamer
Furthermore, you are using code to create aQFileDialoginstance and using that. Which is fine. However there is also astaticmethod 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.
-
@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.