PDA

View Full Version : QValidator, regular expressions and QLineEdit



hvengel
5th August 2007, 21:12
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:


QRegExp validProfileFilenameRegEx("(.*)((\\.icc)|(\\.icm)){1,1}");
QValidator* profileExtensionValidator;
validProfileFilenameRegEx.setCaseSensitive(FALSE);
profileExtensionValidator = new QRegExpValidator(validProfileFilenameRegEx, this);

// OuputProfileEdit is a QLineEdit in a designer form
OutputFileEdit -> setValidator(profileExtensionValidator);

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?

jacek
8th August 2007, 01:25
If I change the regulalr expression to

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

It will not let me enter anything in the QLineEdit.
That's because it's an invalid expression and nothing matches it.

A bit better regular expression is:
QRegExp validProfileFilenameRegEx(".*(?:\\.icc|\\.icm)");
Because () are a bit more expensive than (?: ), which are only for grouping.


Is this even possible or am I wasting my time trying to get this to work?
You can enter whatever you like, because .* matches every possible string. The problem is that validator allows to change the focus in every possible state, including QValidator::Intermediate.