Results 1 to 5 of 5

Thread: QRegExp carriage return

  1. #1
    Join Date
    Feb 2017
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QRegExp carriage return

    I need to restrict the inputs on one of my qlineEdit to a range between 2 numbers (for example "14-52"). The following code works fine for this.
    Qt Code:
    1. QRegExp range( "[0-9]*[-][0-9]*");
    To copy to clipboard, switch view to plain text mode 

    Now I need to add some functionality to allow the user to press enter inside the line edit , in order to launch a command. I use the following for this.
    on_myLineEdit_returnPressed()

    My issue is that the QRegExp blocks the enter command. From the documentation i figured that I have to add \r, but this doesn't work:
    Qt Code:
    1. QRegExp range( "[0-9]*[-][0-9]*[\r]");
    To copy to clipboard, switch view to plain text mode 

    Also, ideally, this should also work when I only give a single number.

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

    Default Re: QRegExp carriage return

    CR character is not added to the QLineEdit content.

  3. #3
    Join Date
    Feb 2017
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QRegExp carriage return

    Well, if I comment out the line which assigns the QRexExpValidator to the myLineEdit, the on_myLineEdit_returnPress() works again, so the problem has to be with the regular expression.
    Last edited by phil333; 16th March 2017 at 17:24.

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QRegExp carriage return

    Your regex, as written, requires the \r be present and as @lesiok commented, it's not added to the QLineEdit, so the regex, with the \r required will never match, right?

    Edit: I think the regex "^(?:([0-9]+)-)?([0-9]+)$" will do what you want minus the carriage return part. Group 1 is the first number to the left of the - and Group 2 is the right hand side. If no - is present, meaning it's not two numbers separated by the -, then Group1 is an empty string and Group 2 would be the number, etc.
    Last edited by jefftee; 17th March 2017 at 00:41.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Feb 2017
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QRegExp carriage return

    Thank you for your solution.
    It works exactly as it should. My initial solution seems to be working too. It seems like I made a mistake while testing. I apologize.
    Your solution does have an advantage, as it forbids me to press enter on en entry like "10-"

    Thank you

Similar Threads

  1. QRegExp carriage return
    By phil333 in forum Newbie
    Replies: 0
    Last Post: 15th March 2017, 16:33
  2. insertPlainText and carriage return
    By RenanBS in forum Newbie
    Replies: 3
    Last Post: 28th August 2012, 18:41
  3. Carriage Return in QString
    By incapacitant in forum Newbie
    Replies: 7
    Last Post: 2nd December 2010, 09:18
  4. Having trouble with carriage return
    By FoleyX90 in forum Newbie
    Replies: 0
    Last Post: 27th May 2010, 16:15
  5. removing carriage return from QString
    By gren15 in forum Qt Programming
    Replies: 3
    Last Post: 28th June 2009, 22:07

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
  •  
Qt is a trademark of The Qt Company.