Results 1 to 7 of 7

Thread: QFileDialog getSaveFileName() doesn't prompt overwrite file confirmation?

  1. #1
    Join Date
    Apr 2009
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QFileDialog getSaveFileName() doesn't prompt overwrite file confirmation?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFileDialog getSaveFileName() doesn't prompt overwrite file confirmation?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2009
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFileDialog getSaveFileName() doesn't prompt overwrite file confirmation?

    Quote Originally Posted by wysota View Post
    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?

  4. #4
    Join Date
    Apr 2009
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFileDialog getSaveFileName() doesn't prompt overwrite file confirmation?

    how to manually ask for overwrite confirmation..i mean how to check the files exist or not?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFileDialog getSaveFileName() doesn't prompt overwrite file confirmation?

    Quote Originally Posted by nonadoes View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Apr 2009
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default

    Quote Originally Posted by wysota View Post
    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?
    Last edited by wysota; 17th April 2009 at 10:06.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFileDialog getSaveFileName() doesn't prompt overwrite file confirmation?

    Quote Originally Posted by nonadoes View Post
    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()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to save file with QFileDialog
    By pnikolov in forum Qt Programming
    Replies: 11
    Last Post: 1st June 2012, 10:23
  2. QFileDialog and file extension
    By calhal in forum Qt Programming
    Replies: 3
    Last Post: 9th March 2010, 12:54
  3. Replies: 2
    Last Post: 28th March 2009, 10:11
  4. Check for invalid file name in QFileDialog
    By darren in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2008, 14:43
  5. QFileDialog and file sequences
    By peterhillman in forum Qt Programming
    Replies: 5
    Last Post: 24th November 2007, 10:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.