0

I have a QLineEdit with an InputMask set to ">AAAA90", that is, I expect the text to consists of exactly 4 uppercase Ascii letters and between 1-2 digits. If the user types "AA1" however, the QLineEdit will show AA 1, namely it will insert two blanks and print the "1" which is permitted in the 5th position. I would rather want a behavior like with illegal characters at any position, namely if the user types "AA%" then the cursor stays at the third position and does not print the "%" character.

Is this possible in QT5?

Reimundo Heluani
  • 918
  • 9
  • 18

2 Answers2

1

Thanks @Mike for the tip on QValidator, I ended up hooking a validator like

 QRegExp rgx("[a-zA-Z]{4}\\d{1,2}");
 QValidator *comValidator = new QRegExpValidator (rgx, this);
 comLineEdit->setValidator(comValidator);

And hooking textEdited with:

void MainWindow::comTextEdited(const QString &arg1)
{
  qobject_cast<QLineEdit*>(sender())->setText(arg1.toUpper());
}

To force the first 4 characters to uppercase.

Reimundo Heluani
  • 918
  • 9
  • 18
0

Note that Input Masks don't insert blank spaces in the returned text (i.e. return value of QLineEdit::text() method), although blank spaces are inserted in GUI for better readability.

To more clarify, Input Mask make QLineEdits to work in overwrite mode not insert mode. But setting a QValidator leaves QLineEdit working in insert mode untouched.

For example if you type "AA3", GUI shows "AA 3" but text() method returns AA3. If you now move the cursor back to 3th position and type "B", the GUI shows "AAB 3" (not "AAB 3", because we are in overwite mode) and text() method returns "AAB3".

frogatto
  • 28,539
  • 11
  • 83
  • 129