PDA

View Full Version : validating input to QLineEdit while seting it through "setText()" method.



netmat
6th April 2010, 13:56
Hi,

I have set a validator as below for a QLineEdit, portLineedit.

QIntValidator* intValidator = new QIntValidator(0, 9999, this);
portLineedit->setValidator(intValidator);

I am setting the text in portLineedit using as QLineEdit::setText() as "Port" from a different function. Now when the portLineedit line edit is being enabled it contains the text "Port". Now I want to modify the text, the validation is not working I am being able to write letters other then integers also and to any number of letters.

The validation is working only when I am deleting the entire content of portLineedit first.

Is it the standard behavior? If not how to make this validation work?


Thanks and regards,

vcp
6th April 2010, 15:37
Hi,

QIntValidator only permits that you type integer numbers.
If you needs join letter and number perhaps you should use inputMask. setInputMask().
See Qt Assistant for more details.

netmat
21st April 2010, 13:45
Hi,

I want to input integer only. I dont want charcter. But the problem is that after setting the QIntValidator also its taking character input.

I think the problem is due to the value I m setting though setText(). I am setting "Port"
port_lineedit->setText("Port"); .
So probably its spoiling the validator. But the validation is working once i delete all the content of the line edit.

Can any body please provide an idea to solve such problem? For being clear I am explaning the problem once more.
"I want a line edit to take only numeric input from the user. but when its disable or empty i want it to display the string"Port".(which i m setting through setText()). I have set QIntValidator validator for it. But its not working for the first time I am going to edit port. I am being able to type numbers,character,whitespace all. this is the problem need to be solved."

Can we do it combining using inputMask and validator? If yes, how?

Thanks and Regards,
~netmat

aamer4yu
21st April 2010, 14:06
May be if you inherit line edit, you might be able to do all that you want.
on key press event you can check whether to accept it or not... and on focus out and focus in events you can toggle the "Port" text.

AnthonySteer
4th December 2020, 05:43
Hi I know it's been a while but did you ever get an answer to this question. I am running into exactly the same issue.

d_stranz
4th December 2020, 17:25
The input mask and validator are used -only- when the user is entering text into the line edit by typing or cutting and pasting. If you are calling setText() from inside your code, then the line edit will let you put anything you want into the edit box. The OP misunderstood this, I think.

Anyway, why would you want to display text like that in a line edit when it is forbidden for the user to type the same text? If you are trying to describe what the line edit should be used for, then either implement a tool tip on it or put a label next to it.

ChrisW67
5th December 2020, 00:11
Anyway, why would you want to display text like that in a line edit when it is forbidden for the user to type the same text? If you are trying to describe what the line edit should be used for, then either implement a tool tip on it or put a label next to it.
The QLineEdit already has a the placeholderText property to provide an in-widget prompt without actually putting the text in the editor's text buffer (since Qt 4.7, Sept 2010).