PDA

View Full Version : QFileDialog getSaveFileName() doesn't prompt overwrite file confirmation?



nonadoes
10th April 2009, 05:29
hey guys,

I'm new to Qt. Anyway, i wanted to use QFileDialog to save file into a directory. So if the file exist before and it should prompt the whether to overwrite msg.

from what i read, it seems that the overwrite msg is automatically enabled already?
but mine didn't prompt the msg. Any idea guys?

Below is my sample code:

QString filePath = QFileDialog::getSaveFileName(NULL, tr("CSV files (*.csv)"), this, NULL, tr("Export To"));

if(filePath.isEmpty())
return;

if(filePath.find(".csv") == -1)
filePath += ".csv";

Thanx

wysota
10th April 2009, 07:51
The dialog will not ask to confirm overwrite if the file that exists contains some extension and you enter the same base name but without the extension. For the filesystem these are two different files. If you're adding the extension manually, you also have to manually ask for overwrite confirmation.

nonadoes
10th April 2009, 09:31
The dialog will not ask to confirm overwrite if the file that exists contains some extension and you enter the same base name but without the extension. For the filesystem these are two different files. If you're adding the extension manually, you also have to manually ask for overwrite confirmation.

Yes, i understand if the extension wasn't there then the system will treat it as a different files. But i did try to key in the extension in the QFileDialog. It also never prompt the overwrite msg box.

As you can see i manually add ".csv" extension after getting the filename because i'm unable to get the filepath + extension together from the QFileDialog. As i read around this forum, one person mention is not built in into QFileDialog rite?

So now, how do i solve this?

nonadoes
10th April 2009, 11:14
how to manually ask for overwrite confirmation..i mean how to check the files exist or not?

wysota
10th April 2009, 14:43
But i did try to key in the extension in the QFileDialog. It also never prompt the overwrite msg box.
Is the file being overwritten after you accept the dialog?


As you can see i manually add ".csv" extension after getting the filename because i'm unable to get the filepath + extension together from the QFileDialog.
If that's so then no wonder there is no overwrite confirmation. The is no clash between a filename and the same filename but with different suffix. They are two different files. If you enter the suffix as well in the dialog, you will get your overwrite confirmation dialog.

If you want to manually ask for overwrite confirmation use QMessageBox.

nonadoes
17th April 2009, 09:36
Is the file being overwritten after you accept the dialog?


If that's so then no wonder there is no overwrite confirmation. The is no clash between a filename and the same filename but with different suffix. They are two different files. If you enter the suffix as well in the dialog, you will get your overwrite confirmation dialog.

If you want to manually ask for overwrite confirmation use QMessageBox.

Sorry for the late reply. Ya, i know if without the extension, it will be treated as a different file. So during QFiledialog prompt for file name to be save, i key in the extension as well, but it also did not prompt for the overwrite message. It overwrite automatically.

Anyway, currently my solution is doing it everything manually.
First get the file name, then check the file name has the extension ".csv" or not. if not add to the filename. Then i use QFile::exists to compare the file name to the current directory.
if exists then prompt QMessageBox::Yes;No;Cancel else save the file.

by the way, any other way to get the file extension user select. My current situation is easy because i only have one. If has more than one extension, how do i get what user has selected?

Ohh Ya. Another thing i want to ask.
How to set the the file Path for "My Documents"? which is a special/common folder.
(eg: Desktop, Application data, Program Files and etc).

Because the file path can be different between PCs. so how to telle this?

wysota
17th April 2009, 10:09
by the way, any other way to get the file extension user select. My current situation is easy because i only have one. If has more than one extension, how do i get what user has selected?
There is an argument in QFileDialog called selectedFilter where you pass a pointer to a QString that will be filled with the selected filter.


Ohh Ya. Another thing i want to ask.
How to set the the file Path for "My Documents"? which is a special/common folder.
(eg: Desktop, Application data, Program Files and etc).

Because the file path can be different between PCs. so how to telle this?

QDesktopServices::storageLocation()