I have a dialog where I allow the user to enter an output file name in a QLineEdit. This allows users to specify an output filename and then run a process (that takes a while to complete) that will output the results to that file. The dialog also has a button to allow the user to select an existing file (to over write) that uses a QFileDialog::getSaveFileName to populate the QLineEdit. Users can also select an existing file and modify what is returned in the QLineEdit to create a new file.

This all more or less works as intended. The issue I have is that I need to make sure that users have selected a valid file name. There are two requirements for this.

First the directory the user has input needs to exist. This is not the issue I am covering here.

Second there are two valid extensions for these files (*,icc and *,icm) and I need to make sure that the user has entered a valid extension and perhaps add it if needed.

I can write a small routine to parse the file name when the user kicks off the file creation process to check both of these and handle any errors. And this is likely what I will end up doing at least to validate the existence of the output directory. But I would prefer to handle validation of the file extesion as the user enters the file name into the QLineEdit if possible.

I have tried to use a QRegExpValidator with the QLineEdit to force the use of the correct file extensions but I can't get it to work. It simply does nothing or it prevents any input by the user. My code looks like this:

Qt Code:
  1. QRegExp validProfileFilenameRegEx("(.*)((\\.icc)|(\\.icm)){1,1}");
  2. QValidator* profileExtensionValidator;
  3. validProfileFilenameRegEx.setCaseSensitive(FALSE);
  4. profileExtensionValidator = new QRegExpValidator(validProfileFilenameRegEx, this);
  5.  
  6. // OuputProfileEdit is a QLineEdit in a designer form
  7. OutputFileEdit -> setValidator(profileExtensionValidator);
To copy to clipboard, switch view to plain text mode 

The above code will let me put anything I want into the QLineEdit. If I change the regulalr expression to

"(.*?)((\\.icc)|(\\.icm)){1,1}"

It will not let me enter anything in the QLineEdit.

What regular expression do I have to use to make the QLineEdit only allow what is entered to end with .icc or .icm? Is this even possible or am I wasting my time trying to get this to work?