Specify default extension in QFileDialog::getSaveFileName - c++

Is there an equivalent of the lpstrDefExt member of OPENFILENAME struct used in the Win32 function GetSaveFileName?
Here's description from MSDN:
LPCTSTR lpstrDefExt
The default extension. GetOpenFileName and GetSaveFileName append this
extension to the file name if the user fails to type an extension.
This string can be any length, but only the first three characters are
appended. The string should not contain a period (.). If this member
is NULL and the user fails to type an extension, no extension is
appended.
So if lpstrDefExt is set to "txt" and the user types "myfile" instead of "myfile.txt", the function still returns "myfile.txt".

Edit: If this does not work for you look at the answer below by #user52366
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 didn't specify one in the dialog. You can see the implementation of this in the qt_win_get_save_file_name() inside the "qfiledialog_win.cpp" Qt source file.
Unfortunately this doesn't work for the getOpenFileName() method.

As mentioned in the comment above, this does not work, at least for me.
In the end I skipped the static method and used the following:
QFileDialog dialog(this, "Save someting", QString(),
"Comma-separated file (*.csv)");
dialog.setDefaultSuffix(".csv");
dialog.setAcceptMode(QFileDialog::AcceptSave);
if (dialog.exec()) {
const auto fn = dialog.selectedFiles().front();
// a QStringList is returned but it always contains a single file
// do something using filename 'fn' ...
}

Not sure what exactly LPCTSTR lpstrDefExt is trying to do but 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

Related

How to save a file with different extension according to Boolean value

I want to save files according to the value of a Boolean I have in my program. if Boolean true extension must be (".me") and if false extension will be (".you").
I am using this line of code to filter my extensions:
QString Save = QFileDialog::getSaveFileName(this, "Save File to:", QDir::currentPath(), tr("DIAG (*.me *.you)" ));
You can do it like this:
QString save = QFileDialog::getSaveFileName(this, "Save File to:", QDir::currentPath(), bool_value ? tr("DIAG (*.me)" : tr("DIAG (*.you)"));

Rename files with same name but differing extensions

If a tickbox is clicked on my application, all files in a specified folder having the same pre-dot name, (eg. TESTCRC32.xxx) will be re-named. If the filename is something else, (eg. Pic.jpg) this file will not be renamed.
How can I go about this? I was thinking a for loop...
void SecondDlg::OnTickBox()
{
// Add code here...
CString oldFile = myPath.Left(myPath.ReverseFind(_T('.')));
rename(oldFile, newFile);
}
You are doing wrong. lets take an example, Suppose myPath is having path "C:\abc\xyz.bmp"
After this line:
CString oldFile = myPath.Left(myPath.ReverseFind(_T('.')));
Now:
oldFile = "C:\\abc\\xyz"; // extension removed
At last you are calling rename
rename(oldFile, newFile); //you can use myPath instead of oldFile
As oldFile = "C:\abc\xyz"; and which is not the correct path, so it is not renamimg the file.
you should pass full path of the file(C:\abc\xyz.bmp).

Setting the name of a text file to a variable in qt

I'm exporting data to a text file in qt every time a run a code. With my current code that file is overwritten each time. My question is how can I set the title to be a variable eg pulse_freq, this way new files will be created based on my variable values. I just can't get the syntax right.
Is there a way to put my files in a folder in the same directory as my build files? I need my code to be cross platform and if I use the full path name it's apparently incompatible with any non-windows OS. If I just name the files there'd be too much clutter in the folder. Relevant code is below:
// Export to data file
QString newname = QString::number(variables.nr_pulses);
QString filename = "C:/Users/BIC User/Documents/BIC Placement Documents/QT_data/Data.txt";
QFile file( filename );
You can just use something along lines:
QString s1 = "something";
QString s2 = " else";
QString s3 = s1 + s2;
QString concatenation with overloaded operator+ works like charm.
And about referencing folder you're in, instead of hardcoding its path, use QDir::currentPath()
Thus, your filename creation should look like the following:
QString folder = QDir::currentPath();
QString file = QString::number(variables.nr_pulses); //or whatever else you want it to be
QString extension = ".txt" // or whatever extension you want it to be
QString full_filename = folder + file + extension;
In order not to mess with appending string after the extension, just separate it into another QString and concatenate those 3 elements as above (folder + file + extension).

Replace single quote in QFile file name

I want open a file which contains single quote but I can't open it.
File name example : QFile file("my'file.example")
I've tried with file.fileName().replace("\'", "\\\'") but it's the same result.
You are trying to replace "\'" but it is not on the original string so it will not work. Furthermore, QFile::filename return a copy of the filename property, and any modification (like replace) will be made on the copy. To play with the filename (before open), use
file.setFilename(file.fileName().myModificationOperation())
Have you tried with QFile file("my\'file.example")?
to test your parameter use the static call:
QString filename = "my\'file.example";
bool okay = QFile::exists(filename);

How to treat a QString as a file location and get its directory

I gather a list of files into a QStringList from a Qt GUI. Each of these files is a .txt file, with a corresponding video file in same_folder_as_txt/videos/.
Is there an easy way to manipulate QString objects as file paths? For example, given C:/some/path/foo.txt , I want to retrieve C:/some/path/videos/foo.avi
Given your path as a QString s
info = QFileInfo(s)
// Get the name of the file without the extension
base_name = info.baseName()
// Add a ".avi" extension
video_file = QStringList((base_name, "avi")).join(".")
// Get the directory
dir_name = info.path()
// Construct the path to the video file
video_path = QStringList((dir_name, QString("videos"), video_file).join("/")
You can convert them each to QDir, perform your modifications as a path, and then use absolutePath() to get the QString back.