Home > Net >  return key doesn't work when i use regex in QT C
return key doesn't work when i use regex in QT C

Time:10-27

Hey guys I'm trying to use regex on line edit in QT but when I use my regex one function that do something when I enter the return key on keyboard doesn't work any more!

This is my regex on line edit:

QRegularExpression r("[0-9\\.\\ \\-\\=\\/\\*\n]{100}");
ui->lineEdit->setValidator(new QRegularExpressionValidator (r,this));

And this is my function test:

void MainWindow::on_lineEdit_returnPressed()
{
    on_pushButton_14_clicked();
}

I also try my regex without "\n" but doesn't change any thing. When I comment the regex my function works correctly.

So any solution?

CodePudding user response:

Your regex needs to support a pattern of length one, thus, {100} quantifier should be replaced with {1,100} or even {0,100}.

Besides, you may also add \r (carriage return) char to the character set, and remove unnecessary escapes:

QRegularExpression r("^[0-9. =/*\n\r-]{1,100}$");

I added ^ and $ anchors to make sure the regex only matches the whole string (here, line).

  • Related