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 inQLineEdit
s usingQRegExpValidator
. 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 myQRegExpValidator
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.
- How to use a regex validator
-
@donnpie
Scribbled in haste.I'm not going to speak about using
Qt4
/QRegExp
/QRegExpValidator
, you should not use them, only Qt5QRegularExpressionValidator
/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 theQLineEdit
. I could be mistaken, but at least try creating yourQRegularExpressionValidator
s with the correspondingQLineEdit
instead of your usingthis
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
QLineEdits
s, confirmed by printing outqDebug() << "UI parent: " << ui->lineEditPages->parent();
I even tried making
ui->lineEditPages
the parent of theQRegularExpressionValidator
, but that didn't work either. -
Hi,
Can you provide a minimal compilable example that shows that issue ?