Unsolved How to prevent space bar event from the QLineEdit
-
I am creating a lineedit which accepts Ip address , I have set input mask for the line edit as,
ui->lineEdit->setInputMask("000.000.000.000");
Now problem is that in the line edit it accepts the space bar also , How to prevent the line edit from not accepting space key press event ?
-
@maniron See documentation(https://doc.qt.io/qt-5/qlineedit.html#inputMask-prop):
"A space character, the default character for a blank, is needed for cases where a character is permitted but not required."
You can use 9 instead of 0, but then you will need to fill with 0 like: 127.000.000.001Another possibility is https://doc.qt.io/qt-5/qlineedit.html#setValidator and https://doc.qt.io/qt-5/qregularexpressionvalidator.html
-
@jsulm Any sample example will be availabe ?
-
@maniron Example for what exactly?
-
@jsulm I dont want space when spacce key is pressed , it should be rejected when space key is pressed in lineedit
-
@maniron I already suggested to use a validator (setValidator) based on regular expression.
-
@jsulm ok i will see through it
-
@jsulm I tried to understand QRegExpValidator But donno whats wrong in this code
QRegExp rx("[0-9]\\d{1,255}"); // the validator treats the regexp as "^[1-9]\\d{0,3}$" QRegExpValidator *ipValidator = new QRegExpValidator(rx, this); //QRegExpValidator v(rx, 0); ui->lineEdit->setValidator(ipValidator);
Can any one help me out in understanding the QRegExp . If I set this validator , Its not accepting any numbers or letters in the lineedit
-
@maniron said in How to prevent space bar event from the QLineEdit:
[0-9]\d{1,255}
This is wrong: {1,255} means between one and 255 occurrences of the character before.
This is your reg exp:([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
Take a look at
http://gamon.webfactional.com/regexnumericrangegenerator/
https://regex101.com -
QRegExp rx("([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])");
Is this right ? cause same problem arises the line edit is not accepting any numbers
-
@maniron You know that you have to escape special characters in C++ string literals?
If you have C++11 compiler you can simply doR"(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))"
-
@jsulm I am using QT 5.5.0
-
-
Hi
try to add
CONFIG += c++11
to your .pro file and run qmake
it should work.
Then you can use the handy R" syntax and avoid
having to escape the exp string. -
@jonb it doesnt show any co mpiler version
-
@maniron
What doesn't show any compiler version? If you are developing with Qt you should know which compiler you chose and what version it is at, that was your choice, not Qt's. -
@maniron The compiler is configured in the Kit.
But actually you could simply copy what I posted and see whether it works... -
@jsulm
I tried adding this but still the line edit is not getting any input
ui->lineEdit->setInputMask("000.000.000.000");QRegExp rx(R"(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))"); // the validator treats the regexp as "^[1-9]\\d{0,3}$" QRegExpValidator *ipValidator = new QRegExpValidator(rx, this); ui->lineEdit->setValidator(ipValidator);
-
@maniron said in How to prevent space bar event from the QLineEdit:
line edit is not getting any input
Do you mean you can't input anything?
-
@jsulm yes