Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLineEdit using a QRegExpValidator doesn't validate in English
Forum Updated to NodeBB v4.3 + New Features

QLineEdit using a QRegExpValidator doesn't validate in English

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 524 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    donnpie
    wrote on last edited by
    #1

    Hi guys,
    What should be straightforward now has me completely baffled. I'm trying to validate user input in QLineEdits using QRegExpValidator. There are four validators, however, none of them have any effect, in other words, they accept any input. I'm coding in Qt 5.12. The program compiles just fine - no errors, no warnings.

    What I've tried so far:

    • How to use a regex validator
      -- I made sure I created my QRegExpValidator objects on the heap
    • QLineEdit using a QRegExpValidator doesn't take effect
      -- I also tried the Qt5 Syntax as suggested by @SGaist, but that I still get the same results
    • I've tested my regexs here, so I know the syntax is correct
    • I've made sure to double escape the \ characters
    • I've printed the address of the QRegExpValidator object to qDebug(), so I know the object exists in memory

    Here's the code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    //mainwindow.cpp
    
    #define AUTHOR_RX "^[A-Z]+$" //Test regex: Only allow capital letters
    #define TITLE_RX "\\w+"      //only allow words
    #define JOURNAL_RX "\\w+"    //only allow words
    #define PAGES_RX "[123456789][123456789]{0,3}\\s{0,1}-\\s{0,1}[123456789][123456789]{0,3}" //Example: 12 - 24
    
    MainWindow::MainWindow(QWidget *parent, ReferenceModel *m) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        //Setup regex's and validators
        //Qt4 syntax
        QRegExp authorRx(AUTHOR_RX);
        QRegExp titleRx(TITLE_RX);
        QRegExp journalRx(JOURNAL_RX);
        QRegExp pagesRx(PAGES_RX);
        QRegExpValidator *authorVal = new QRegExpValidator(authorRx, this);
        QRegExpValidator *titleval = new QRegExpValidator(titleRx, this);
        QRegExpValidator *journalVal = new QRegExpValidator(journalRx, this);
        QRegExpValidator *pagesVal = new QRegExpValidator(pagesRx, this);
        ui->lineEditAuthor->setValidator(authorVal);
        ui->lineEditTitle->setValidator(titleval);
        ui->lineEditJournal->setValidator(journalVal);
        ui->lineEditPages->setValidator(pagesVal);
    
        qDebug() << "QRegExpValidator pointer address: " << &pagesVal;
        qDebug() << "QRegExpValidator target address: " << pagesVal;
        qDebug() << "UI QRegExpValidator target address: " << ui->lineEditPages->validator(); //Confirm it is pointing to the right place
    
    
    /*  //Qt5 syntax - has no effect
        QRegularExpression authorRx(AUTHOR_RX);
        QRegularExpression titleRx(TITLE_RX);
        QRegularExpression journalRx(JOURNAL_RX);
        QRegularExpression pagesRx(PAGES_RX);
        QRegularExpressionValidator *authorVal = new QRegularExpressionValidator(authorRx, this);
        QRegularExpressionValidator *titleval = new QRegularExpressionValidator(titleRx, this);
        QRegularExpressionValidator *journalVal = new QRegularExpressionValidator(journalRx, this);
        QRegularExpressionValidator *pagesVal = new QRegularExpressionValidator(pagesRx, this);
        ui->lineEditAuthor->setValidator(authorVal);
        ui->lineEditTitle->setValidator(titleval);
        ui->lineEditJournal->setValidator(journalVal);
        ui->lineEditPages->setValidator(pagesVal);
        */
    //Other stuff...
    

    Any ideas will be appreciated.

    Thanks.

    JonBJ 1 Reply Last reply
    0
    • D donnpie

      Hi guys,
      What should be straightforward now has me completely baffled. I'm trying to validate user input in QLineEdits using QRegExpValidator. There are four validators, however, none of them have any effect, in other words, they accept any input. I'm coding in Qt 5.12. The program compiles just fine - no errors, no warnings.

      What I've tried so far:

      • How to use a regex validator
        -- I made sure I created my QRegExpValidator objects on the heap
      • QLineEdit using a QRegExpValidator doesn't take effect
        -- I also tried the Qt5 Syntax as suggested by @SGaist, but that I still get the same results
      • I've tested my regexs here, so I know the syntax is correct
      • I've made sure to double escape the \ characters
      • I've printed the address of the QRegExpValidator object to qDebug(), so I know the object exists in memory

      Here's the code:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      //mainwindow.cpp
      
      #define AUTHOR_RX "^[A-Z]+$" //Test regex: Only allow capital letters
      #define TITLE_RX "\\w+"      //only allow words
      #define JOURNAL_RX "\\w+"    //only allow words
      #define PAGES_RX "[123456789][123456789]{0,3}\\s{0,1}-\\s{0,1}[123456789][123456789]{0,3}" //Example: 12 - 24
      
      MainWindow::MainWindow(QWidget *parent, ReferenceModel *m) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          //Setup regex's and validators
          //Qt4 syntax
          QRegExp authorRx(AUTHOR_RX);
          QRegExp titleRx(TITLE_RX);
          QRegExp journalRx(JOURNAL_RX);
          QRegExp pagesRx(PAGES_RX);
          QRegExpValidator *authorVal = new QRegExpValidator(authorRx, this);
          QRegExpValidator *titleval = new QRegExpValidator(titleRx, this);
          QRegExpValidator *journalVal = new QRegExpValidator(journalRx, this);
          QRegExpValidator *pagesVal = new QRegExpValidator(pagesRx, this);
          ui->lineEditAuthor->setValidator(authorVal);
          ui->lineEditTitle->setValidator(titleval);
          ui->lineEditJournal->setValidator(journalVal);
          ui->lineEditPages->setValidator(pagesVal);
      
          qDebug() << "QRegExpValidator pointer address: " << &pagesVal;
          qDebug() << "QRegExpValidator target address: " << pagesVal;
          qDebug() << "UI QRegExpValidator target address: " << ui->lineEditPages->validator(); //Confirm it is pointing to the right place
      
      
      /*  //Qt5 syntax - has no effect
          QRegularExpression authorRx(AUTHOR_RX);
          QRegularExpression titleRx(TITLE_RX);
          QRegularExpression journalRx(JOURNAL_RX);
          QRegularExpression pagesRx(PAGES_RX);
          QRegularExpressionValidator *authorVal = new QRegularExpressionValidator(authorRx, this);
          QRegularExpressionValidator *titleval = new QRegularExpressionValidator(titleRx, this);
          QRegularExpressionValidator *journalVal = new QRegularExpressionValidator(journalRx, this);
          QRegularExpressionValidator *pagesVal = new QRegularExpressionValidator(pagesRx, this);
          ui->lineEditAuthor->setValidator(authorVal);
          ui->lineEditTitle->setValidator(titleval);
          ui->lineEditJournal->setValidator(journalVal);
          ui->lineEditPages->setValidator(pagesVal);
          */
      //Other stuff...
      

      Any ideas will be appreciated.

      Thanks.

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

      @donnpie
      Scribbled in haste.

      I'm not going to speak about using Qt4/QRegExp/QRegExpValidator, you should not use them, only Qt5 QRegularExpressionValidator/QRegularExpressionValidator.

      That apart, I think (but not certain) that just the other day we discovered it is not enough to go QLineEdit::setValidator(validator), validator must have parent of the QLineEdit. I could be mistaken, but at least try creating your QRegularExpressionValidators with the corresponding QLineEdit instead of your using this as parent? If that makes no difference, ignore me... :)

      1 Reply Last reply
      1
      • D Offline
        D Offline
        donnpie
        wrote on last edited by
        #3

        Thanks for the feedback, @JonB. Sadly no success.

        I changed the code back to Qt5 style. This structure of my UI is like this: MainWindow>centralWidget>gridLayout, so gridLayout is the parent of the QLineEditss, confirmed by printing out

        qDebug() << "UI parent: " << ui->lineEditPages->parent();
        

        I even tried making ui->lineEditPages the parent of the QRegularExpressionValidator, but that didn't work either.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Can you provide a minimal compilable example that shows that issue ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

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