Results 1 to 2 of 2

Thread: QValidator, regular expressions and QLineEdit

  1. #1
    Join Date
    Mar 2006
    Posts
    74
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X Unix/X11 Windows

    Default QValidator, regular expressions and QLineEdit

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QValidator, regular expressions and QLineEdit

    Quote Originally Posted by hvengel View Post
    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:
    Qt Code:
    1. QRegExp validProfileFilenameRegEx(".*(?:\\.icc|\\.icm)");
    To copy to clipboard, switch view to plain text mode 
    Because () are a bit more expensive than (?: ), which are only for grouping.

    Quote Originally Posted by hvengel View Post
    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.

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.