PDA

View Full Version : QRegExp carriage return



phil333
16th March 2017, 14:45
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.

QRegExp range( "[0-9]*[-][0-9]*");

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:

QRegExp range( "[0-9]*[-][0-9]*[\r]");

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

Lesiok
16th March 2017, 15:06
CR character is not added to the QLineEdit content.

phil333
16th March 2017, 15:57
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.

jefftee
17th March 2017, 01:25
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.

phil333
20th March 2017, 10:55
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