Specify the default extension in QFileDialog :: getSaveFileName

Is there an equivalent to the lpstrDefExt element of the OPENFILENAME structure used in the Win32 GetSaveFileName function?

Here is a description from MSDN:

LPCTSTR lpstrDefExt

The default extension. GetOpenFileName and GetSaveFileName add this file name extension if the user cannot enter the extension. This string can be of any length, but only the first three characters are attached. The string must not contain a period (.). If this member is NULL and the user cannot enter the extension, no extension will be attached.

So, if lpstrDefExt is set to "txt" and the user enters "myfile" instead of "myfile.txt", the function still returns "myfile.txt".

+4
source share
2 answers

Qt will extract the default extension from the "selectedFilter" parameter, if specified.

Here is an example:

QString filter = "Worksheet Files (*.abd)"; QString filePath = QFileDialog::getSaveFileName(GetQtMainFrame(), tr("Save Worksheet"), defaultDir, filter, &filter); 

When using this code, the getSaveFileName () method will automatically add the .abd file extension if the user did not specify it in the dialog box. You can see the implementation of this in the qt_win_get_save_file_name () file inside the Qt source file qfiledialog_win.cpp.

Unfortunately, this does not work for the getOpenFileName () method.

+6
source

Not sure what LPCTSTR is trying to execute lpstrDefExt, but the Qt documentation gives the following example

  QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "/home/jana/untitled.png", tr("Images (*.png *.xpm *.jpg)")); 

http://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName

+1
source

Source: https://habr.com/ru/post/1369543/


All Articles