Nhatz HK Nhatz HK - 1 month ago 18
C++ Question

how do I use QRegularExpression in Qt

I searched (an incredible amount of time) through Qt Documentation and some other documentation online but I can't get an answer.

I'm using a QLineEdit which will take a "C++ class name" (that means it should accept only [a-b](<-uppercase as well), [0-9] and underscore(_). No matter how (blindly) I try to set it up, it doesn't work.

1 m_classDefLayout->addRow("Class Name",m_className);
2 m_className->setValidator(m_nameValidator);
3 m_nameValidator->setRegularExpression(*m_nameRegExp);
4 //m_namePattern=();
5 m_nameRegExp->setPattern(m_namePattern);


What do I insert in the brackets of line 4? I'm not asking for code to copy and paste. Some examples (even one well explained) can help me so I'll finally finish my project.

Answer

Because I don't know the data type in your code, I just write a simple sample code.

QRegularExpression rx("^[A-Za-z_][A-za-z0-9]*");
QValidator *validator = new QRegularExpressionValidator(rx, this);

ui.lineEdit->setValidator(validator);

If you read below link, you will be accurately understood.

link for ^(caret)

link for *(asterisk)

Comments