PDA

View Full Version : get to know if click yes on replace warning dailog during getSaveFileName()



prasad.ece2
24th December 2013, 11:12
hi,
when I use getSaveFileName(), it will launch save dialog and if I give any existing file name and try to save,
it will lanunch replace warning dailog, here how can I know whether user clicked Yes/No .

Thanks in advance .

ChrisW67
24th December 2013, 11:43
You can't directly. If the user presses Yes then the dialog closes and the file name is returned, just the same as if the file did not exist when the user clicked Ok. If the user presses No the dialog does not close and the user can select another file name. If the user clicks Cancel the dialog closes and an empty string is returned.

If you get a non empty string, and the file exists then the user must have clicked Yes when prompted.

prasad.ece2
24th December 2013, 16:53
could you please elaborate it, i am not able to get it properly...

anda_skoa
24th December 2013, 19:26
If getSaveFileName() returns a non-empty string, i.e. a filename, then the user has either entered a new file name or OKed the overwriting of an existing file.
So you can safely assume that it is OK to write to that file.

Cheers,
_

prasad.ece2
26th December 2013, 06:39
my requirement is I need to call certain function, only if file is overwritten, I have to ignore the case of creating new file.

prasad.ece2
26th December 2013, 11:23
in both cases, if file exist(& click on yes for replace warning dailog) or if file does not exists(&click on save) getSaveFileName() will return a non empty string only.. so how can we come to know that warning dailog appears and user clicked on yes. (i need the situation exactly warning dailog appears & user clicks on yes, I have to ignore the situation if file created first time) .

anda_skoa
26th December 2013, 14:07
I don't see where you can possibly have any problem left.

If the chosen file does not exist, then the user is not asked for overwrite.
If the chosen file does exist, the user is asked and the filename is only returned if overwrite has been confirmed.

So, by pure logicical deduction, the defining thing is the existance of the file, right?

Cheers,
_

prasad.ece2
27th December 2013, 10:54
in both cases, file exist or not exist, the return value of getSaveFileName() is not empty string only (i.e file name of newely created file/file name that was replaced).
with this return value(file name) how can I say that this is the file got replace or this is the file newly created.

stampede
27th December 2013, 11:28
with this return value(file name) how can I say that this is the file got replace or this is the file newly created
You have a wrong assumption that this method is modifying the filesystem. It only returns path choosen by the user, it doesn't modify / create any files.

prasad.ece2
27th December 2013, 11:53
yes stampede, now I came to the mistake. thank you.

d_stranz
31st December 2013, 22:35
So if you really need to know if the user chose to overwrite an existing file, then you can use QFileInfo::exists():



QString fileName = QFileDialog::getOpenFileName( ... );
if ( !fileName.isEmpty() )
{
QFileInfo fileInfo( fileName );
if ( fileInfo.exists() )
{
// then the user chose an existing file
}
else
{
// the user chose to create a new file
}
}