1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nhatz HK
  • 42
  • 2
  • 11

1 Answers1

3

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)

hyun
  • 2,135
  • 2
  • 18
  • 20
  • 'm_className is a QLineEdit ' 'm_nameValidator is my QRegularExpressionValidator ' 'm_nameRegExp is my QRegularExpression ' 'm_namePattern is a QString ' 'And thanks for your answer. ' I assume the brackets following the "^" is for the beginning of the input and the last ones for the end. Am I wrong? – Nhatz HK Oct 26 '16 at 12:08
  • @NhatzHK, I've edited my answer. '^ is for the beginning' is correct, but * is not for the last. $ is for the last. – hyun Oct 26 '16 at 12:39