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?


  • Qt Champions 2017

    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



  • @mrjj Outstanding, this is exactly what I need. Thank you so much for your help. It took a minute to find some python examples, but I have it working. Again thank you for your support!!!

    Michael.



  • @nightpoison
    If you are starting out now and choosing to use Qt's regular expressions (I use Python's), you really should use the newer QRegularExpression class (http://doc.qt.io/qt-5/qregularexpression.html), rather than QRegExp.

    Your example is indeed best handled via a numeric range validator, but there are plenty of other cases where a regular expression is suitable.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.