Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

TextInput how to block entering new data if current value is out of range?



  • I'm looking for way to block entering new data if current value if out of range. I was check the TextInput validator property with a range but setting it just block possibility to press enter, not input new.

    I was tried to use onTextEdited and remove last character but new number is added after this, so in the end always remove one before last digit. Any idea?

    						onTextEdited:
    						{
    							if ((text < analogValid.bottom) ||  (text > analogValid.top))
    							{
    								console.log("onTextEdited: " + text);
    								textInputComp.remove(selectionEnd-1,  selectionEnd);
    							}
    						}


  • IIm not sure if is it a proper way but i solved my problem. I just save the old value of text property in by buffer onTextEdited .
    The onTextEdited will be called before text will be changed. After that text will be changed and automaticly callingonTextChanged where text value will be checked.

    onTextEdited:
    {
    	if ((validator === analogValid) && (text >= analogValid.bottom) && (text <= analogValid.top))
    		oldTextVal = text;
    }
    
    onTextChanged:
    {
    	if ((validator === analogValid)  && ((text < analogValid.bottom) || (text > analogValid.top)))
    		text = oldTextVal;
    }
    

Log in to reply