Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PyQt - Need help with QLineEdit QRegExp better understanding of what I'm seeing
QtWS25 Last Chance

PyQt - Need help with QLineEdit QRegExp better understanding of what I'm seeing

Scheduled Pinned Locked Moved Solved Language Bindings
4 Posts 3 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nightpoison
    wrote on last edited by SGaist
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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

      N 1 Reply Last reply
      4
      • mrjjM mrjj

        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

        N Offline
        N Offline
        nightpoison
        wrote on last edited by
        #3

        @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.

        JonBJ 1 Reply Last reply
        0
        • N nightpoison

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved