PDA

View Full Version : QFileDialog getSaveFileName() doesn't convey choice to NOT OVERWRITE existing file



philw
28th March 2009, 09:00
With the use of the static QFileDialog getSaveFileName() method -- if the file chosen by user already exists, the user is given the choice of not overwriting that file. (This is with Qt 4.3.3).

http://www.thirdtablet.com/cadswes/2009/TextOutput/screenshots/OverwriteFileConfirm.png

However, there doesn't seem to be a way for the caller to know that the user made that choice (of aborting the save operation, being that the file already exists).

When the user CANCELS the selection operation (before making the selection), an empty string is returned as the file path result. An empty string should probably also be the result in the case of the user overwrite denial. But it isn't. See code sample, below.

Is there a well known global entity to check for this user response? Currently this is an unavoidable bug: the file gets overwritten regardless of the user's choice !!!

Test code:


...
static const QString caption ("Select Report Output File");

QString startPathStr = ... // details omitted

static QString selectedFilter ("");
static const QString filter ("HTML (*.htm *.html);;"
"TEXT (*.txt);;"
"All (*)");

// **************************************************
// *** Show and Execute Save File Path Selector ***
// **************************************************

const QString userPickedPath =
QFileDialog::getSaveFileName (NULL, // parent widget
caption,
startPathStr,
filter,
&selectedFilter);

const bool userPickedEmpty (userPickedPath.isEmpty());

std::cout << " Picked: '" << qPrintable (userPickedPath) << "' "
<< (userPickedEmpty ? "[EMPTY]" : "[GOOD TO GO]")
<< std::endl;

if (userPickedEmpty)
{
// File selection canceled by user.
return (false);
//----------->>
}

setReportPath (userPickedPath);
return (true);
//---------->>


Thank you in advance,
Phil Weinstein, CADSWES
http://cadswes.colorado.edu/

aamer4yu
28th March 2009, 09:10
You can put the check yourself after you get the save file name ? cant you ?
Also there might be so many cases, so its expected of the function to only return a file name, and not overwrite it :)

philw
28th March 2009, 10:11
You can put the check yourself after you get the save file name ? cant you ?
Also there might be so many cases, so its expected of the function to only return a file name, and not overwrite it :)

Well, good idea. I can DISABLE the overwrite query popup that is automatically implemented by QFileDialog::getSaveFileName(). That can be done with the optional QFileDialog::Options parameter, bitmask value: QFileDialog:: DontConfirmOverwrite, and checking the existance of the selected file with QFileInfo, and showing my own confirmation dialog box if the file does exist. I guess I'll do that.

But this REALLY IS a BUG (a design oversight, I think) in the Confirm-Overwrite feature of QFileDialog:: getSaveFileName(), -- enabled by default. It's a pretty bad bug because the file WILL be overwritten by the client code because -- if this is true -- the client code has no way of being informed of the user's choice to ABORT the write operation. Is this true?