Results 1 to 17 of 17

Thread: qml and regexp; non-repeating digits and no leading zero

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qml and regexp; non-repeating digits and no leading zero

    Thanks for you answer! this is the regular expression i was looking for. it passes my tests on the page you provided

    now i'm trying to implement it
    ui->setupUi(this);
    QRegExp regex("^([1-9])(?!\\1)([0-9])(?!\\1|\\2)([0-9])(?!\\1|\\2|\\3)([0-9])$");
    QValidator *validator = new QRegExpValidator(regex, this);
    ui->lineEdit->setValidator(validator);
    ui->lineEdit->setFocus();

    but lineEdit only accepts first digit...do you maybe know why?

    i followed the example that i've found in the documentation
    // regexp: optional '-' followed by between 1 and 3 digits
    QRegExp rx("-?\\d{1,3}");
    QValidator *validator = new QRegExpValidator(rx, this);

    QLineEdit *edit = new QLineEdit(this);
    edit->setValidator(validator);

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml and regexp; non-repeating digits and no leading zero

    QRegExp vs QRegularExpression. Read about differences between the two.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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: qml and regexp; non-repeating digits and no leading zero

    Quote Originally Posted by djvujke View Post
    but lineEdit only accepts first digit...do you maybe know why?
    QRegExp doesn't understand or fully implement Perl compatible regular expressions, so when it gets to the next negative look-ahead, it doesn't understand it.

    You can stop trying to make the regex I gave you work with QRegExp as it simply won't work. If you are using Qt 5.1 or higher, use QRegularExpressionValidator instead of QRegExpValidator.

    If you are still using Qt 4.x, then sorry, I don't have a regex for you that will accomplish what you want using QRegExp.

    Edit: I do see where QRegExp supposedly supports pcre negative lookaheads (?!...) but it simply doesn't appear to work. It works fine with QRegularExpression though.
    Last edited by jefftee; 19th March 2015 at 22:35.

  4. #4
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qml and regexp; non-repeating digits and no leading zero

    Thanks

    This is working code
    QRegularExpression regex("^([1-9])(?!\\1)([0-9])(?!\\1|\\2)([0-9])(?!\\1|\\2|\\3)([0-9])$");
    QValidator *validator = new QRegularExpressionValidator(regex, this);
    ui->lineEdit->setValidator(validator);


    case closed

Similar Threads

  1. Display more than four digits/characters
    By StarRocks in forum Newbie
    Replies: 8
    Last Post: 2nd February 2013, 09:12
  2. All digits of float to QString
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2012, 07:20
  3. auto-repeating arrows
    By skrzypu in forum Qt Programming
    Replies: 0
    Last Post: 15th October 2008, 09:32
  4. setNum() digits question
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2008, 15:19
  5. coordinal significant digits
    By nitriles in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2007, 15:54

Tags for this Thread

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.