PyQt - Need help with QLineEdit QRegExp better understanding of what I'm seeing
-
Hello, First time here. I just started to looking at GUI development and have pulled together my first GUI on PyQt. One of the things I'm doing now is setting up parameters regarding user input. I came across masks and maxlength, but I'm trying to better understand QRegExp. I've done some research on Regular Expressions and it makes sense, but I'm still having difficulties doing something that appears to be simple.
for instance I'm trying to limit the input of a QEditLine to only numbers 0-180. This is my code.
Label = QLabel("Shift") labelEdit = QLineEdit() self.labelEdit.setAlignment(Qt.AlignRight|Qt.AlignVCenter) self.labelEdit.setInputMask("999")
I've found the following code online that should limit the range from 0-27, but it doesn't even do that.
editor = QLineEdit(parent) regex = QRegExp(r"(? : 0 [1-9] | 1[0124 - 9] | 2[0-7]") validator = QRegExpValidator(regex, parent) editor .setValidator(validator)
but when I run the code it still allows me to enter numbers above 27
what am I missing?
-
Hi and welcome to the forums
I cant tell whats wrong with your regular expression but for range check, there is a dedicated validator for that.
http://doc.qt.io/qt-5/qintvalidator.html -
@nightpoison
If you are starting out now and choosing to use Qt's regular expressions (I use Python's), you really should use the newerQRegularExpression
class (http://doc.qt.io/qt-5/qregularexpression.html), rather thanQRegExp
.Your example is indeed best handled via a numeric range validator, but there are plenty of other cases where a regular expression is suitable.