'QLineEdit: how to put cursor at the end of typed text?
There is a QLineEdit in which you need to enter a phone number. Gave him a mask
ui->lineEdit_4->setInputMask("+7\\(999\\)999\\-99\\-99;_");
Further, when saving the data, the entered string is checked by the validator:
QRegularExpression numberRegex ("^\\+\\d{1,1}\\(\\d{3,3}\\)\\d{3,3}\\-\\d{2,2}\\-\\d{2,2}$");
QRegularExpressionValidator *numberValidator = new QRegularExpressionValidator (numberRegex);
QString a = ui->lineEdit_4->text();
int b = ui->lineEdit_4->cursorPosition();
if(numberValidator->validate(a, b) == QValidator::Acceptable){
....
}
Above I described everything that is, and now the very essence of the problem. Typing is extremely inconvenient, since the cursor is not attached to anything. Wherever you click, the cursor will appear there. How to make the cursor appear where you need to type text? So far, in ideas there is only a check for the cursor position, and every time it changes, put it on the first "_" in the line. But something sounds crazy
Solution 1:[1]
Connect this signal: https://doc.qt.io/qt-5/qlineedit.html#cursorPositionChanged
In the target method, move cursor to the end of the field with this: https://doc.qt.io/qt-5/qlineedit.html#end
Something like this:
QObject::connect(
ui->lineEdit_4,
&QLineEdit::cursorPositionChanged,
this,
[this](){
ui->lineEdit_4->end(false);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hyde |
