Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLineEdit using a QRegExpValidator doesn't validate in English



  • 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:

    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.



  • @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... :)



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


  • Lifetime Qt Champion

    Hi,

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


Log in to reply