Asked the last question, regarding the mask and how to put the cursor at the end of the typed text, but did not receive an answer. Trying to figure it out on my own, I realized that the question was asked very superficially. So. I tried to delve into the logic, looked at examples on different sites.
ui->lineEdit_newClientPhone->setInputMask("+7\\(999\\)999\\-99\\-99;_");
What was my original idea - when you click the mouse, or when you get focus, check the line in a loop and put the cursor in the place of the first "__". The idea failed very quickly, as soon as I debugged the line ui->lineEdit->text() , and realized that the line consists only of the characters that are in the mask, without the "filler" (_). As a result, I had the line +7()--. The next idea was this, after much torment: I tried to come up with some complex mathematical calculations, like going through a cycle from the end of the string, as soon as part of the string becomes not equal to '(' or ')' or '-' - calculate the position cursor. (considering, again, the characters "( ) -") It seems that something even happened:
if(event->type() == QEvent::MouseButtonPress){
int a = ui->lineEdit_newClientPhone->text().length();
switch(a){
case 6: case 7: case 8:
ui->lineEdit_newClientPhone->setCursorPosition(a - 3);
break;
case 9: case 10:
ui->lineEdit_newClientPhone->setCursorPosition(a - 2);
break;
case 11: case 12:
ui->lineEdit_newClientPhone->setCursorPosition(a - 1);
break;
case 13: case 14:
ui->lineEdit_newClientPhone->setCursorPosition(a);
break;
}
}
But still hit some strange behavior, because when you click the mouse, the cursor is STILL put in the place where the mouse was poked. Despite the fact that putting the above switch into a button seems to work correctly. I even tried to process elementary logic - when receiving focus, the cursor would receive index 4, that is, it would move to the first position necessary for introduction - but here the situation is the same as described above.
Maybe i need to use the validator directly with the line? Now there is a validator, here is its code:
QRegularExpression numberRegex ("^\\+\\d{1,1}\\(\\d{3,3}\\)\\d{3,3}\\-\\d{2,2}\\-\\d{2,2}$");
QRegularExpressionValidator *numberValidator = new QRegularExpressionValidator (numberRegex);
But it is used to check the correct spelling of a phone number. Further in the code, I check the validity of the entered string and then perform the necessary logic.
if(numberValidator->validate(a, b) == QValidator::Acceptable){
...
}
And what can I do? What is a more competent, and most importantly working, solution for creating the correct filling in of a phone number with a mask? In general, without all these "frills" - the mask performs its functions, but insofar as a large number of numbers will need to be entered in the program, managers will not be very happy with the need to aim the mouse at the required place for dialing.