Results 1 to 3 of 3

Thread: Check for invalid file name in QFileDialog

  1. #1

    Default Check for invalid file name in QFileDialog

    Hi,
    I am implementing a Export file dialog inheriting from QFileDialog. I set the AcceptMode to AcceptSave, launch the dialog using exec() and retrieve the file name entered by user from selectedFiles() correctly.

    However, I found out that it seems like the QFileDialog does not trap for the invalid file name. e.g. User is able to enter "abc|sss" and click Ok button, no error message is prompted and the dialog is closed. I would like to show the user a message like "The file name 'abc|sss' is an invalid file name" and the dialog is not closed. In order to do this, I will need to change the code in QFileDialog Ok button handler?

    There is no such problem for an open file dialog because the ok button will be disabled whenever the file name entered does not exists.

    One way for me to fix this is to check the file name myself after the exec() and prompt an error message. But it seems a bit weird to the user as the dialog is closed, another error message pop up, then he will need to reopen the dialog again.

    Is there any way I can achieve this behavior in QT?

    Thanks in advance

  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: Check for invalid file name in QFileDialog

    Quote Originally Posted by darren View Post
    However, I found out that it seems like the QFileDialog does not trap for the invalid file name. e.g. User is able to enter "abc|sss" and click Ok button, no error message is prompted and the dialog is closed. I would like to show the user a message like "The file name 'abc|sss' is an invalid file name" and the dialog is not closed. In order to do this, I will need to change the code in QFileDialog Ok button handler?
    The best way is to validate the name in your code and if it is invalid, show the dialog again yourself.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Check for invalid file name in QFileDialog

    Create Your accept() method. When file name is OK call QFileDialog::accept(), if no show error message and return.

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. QFileDialog and file sequences
    By peterhillman in forum Qt Programming
    Replies: 5
    Last Post: 24th November 2007, 10:16
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. How to check a file for changes since last save
    By nmather in forum General Programming
    Replies: 2
    Last Post: 21st April 2007, 23:43

Tags for this Thread

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.