Character filtering in a text field

Find and share HowTos to various installations / configurations!
7 posts • Page 1 of 1
jtillier
Posts:3
Joined: Thu Nov 26, 2020 5:42 pm

Character filtering in a text field

Post by jtillier »

Hello,

I am looking for a way to filter characters in a text_field.

When a customer writes something, I want to convert it in upper case and keep only some characters, the other characters must be deleted instantly.

The following function in the TextChanged event of the text field works well but only when a character is added at the end of the string. If a character is added in the middle of the string, the caret moves at the end of the string.
I need to find a way to retrieve the position of the caret at the beginning of my function and a way to move the caret to the wanted position at the end.

I tried to use the inputmask function but it does not work as I want it to.

Here are my allowed characters : abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:=/-><

I need the characters to be uppercased automatically or suppressed automatically if they don't belong to the previous list.

Thanks for your help


------------------------------------------------------------------------------------------

main(string newText)
{
string sInput, sCheck, sChar;
bool bReturn;

DebugN("newtext",newText);

sInput = this.text;
sChar = sInput.right(1);

//Liste des caractères autorisés
sCheck = "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:=/->< ]";

bReturn = patternMatch(sCheck, sChar);

// si le dernier caractère saisi fait parti de la liste des caractères autorisés
// on le passe en majuscule
// sinon on le supprime
if (bReturn)
sInput = strtoupper(this.text);
else
sInput.replace(sChar,"");
this.text = sInput;

// on surligne le champ texte s'il n'est pas vide
if (strlen(this.text)>0)
{
setValue(myModuleName()+"."+myPanelName()+":ENVOI_MG1.RECTANGLE", "visible", true);
}
else
{
setValue(myModuleName()+"."+myPanelName()+":ENVOI_MG1.RECTANGLE", "visible", false);
}
majVarSelection();

}

kilianvp
Posts:443
Joined: Fri Jan 16, 2015 10:29 am

Re: Character filtering in a text field

Post by kilianvp »

Code: Select all

void checkPatternMatch()
{
 string pattern = this.text;
 int i;
 string s;
 string sTemp;

 for ( i=1; i<= strlen(pattern); i++)
 {
   sTemp = substr(pattern,i-1,1);
   if ( patternMatch("[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:=/->< ]",sTemp))
   {
     s += sTemp;
   }

 }
 this.text = strtoupper(s);
}

main(string newText)
{
 checkPatternMatch();

// on surligne le champ texte s'il n'est pas vide
...
}

jtillier
Posts:3
Joined: Thu Nov 26, 2020 5:42 pm

Re: Character filtering in a text field

Post by jtillier »

Thanks a lot for your answer, it works well.

I just need one more feature : when I insert text in the middle of my string, the caret goes to the end of the string each time I type a character... Is there a way to keep the caret at his last position ?

kilianvp
Posts:443
Joined: Fri Jan 16, 2015 10:29 am

Re: Character filtering in a text field

Post by kilianvp »

no

gschijndel
Posts:376
Joined: Tue Jan 15, 2019 3:12 pm

Re: Character filtering in a text field

Post by gschijndel »

The inputMask should be able to do it, but unfortunately is only evaluated when the return key is pressed. What is not working?

jtillier
Posts:3
Joined: Thu Nov 26, 2020 5:42 pm

Re: Character filtering in a text field

Post by jtillier »

Hello, thanks for your help.

With "inputmask" I couldn't filter all the characters I want (maybe I misunderstood how this function works).

My textfield is 15 characters string. Here are my allowed characters :
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:=/-><

The inputmask function allows a mask character for each character of the string. It works for letters and numbers with the mask ">nnnnnnnnnnnnnnn" but i need to allow as well :=/-><
I don't know how to do it.

gschijndel
Posts:376
Joined: Tue Jan 15, 2019 3:12 pm

Re: Character filtering in a text field

Post by gschijndel »

When looking at the examples of the QLineEdit inputMask documentation it seems that it meant for inputs like ip/mac address, date/time or MLFB numbers.
Too bad no validator can be set as the QRegularExpressionValidator seems to provide the best solution for what you want. Perhaps request this feature.

7 posts • Page 1 of 1