PDA

View Full Version : Check for invalid file name in QFileDialog



darren
7th November 2008, 10:04
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

wysota
7th November 2008, 11:00
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.

Lesiok
7th November 2008, 14:43
Create Your accept() method. When file name is OK call QFileDialog::accept(), if no show error message and return.