QLineEdit accept comma delimited integers
-
I want to use QLineEdit and set it up to only accept a comma delimited set of integers, is this possible and how do I do it?
More specifically, I have a minimum and maximum value, I want to check that the numbers allowed are between the minimum and maximum and comma delimited.
-
I want to use QLineEdit and set it up to only accept a comma delimited set of integers, is this possible and how do I do it?
More specifically, I have a minimum and maximum value, I want to check that the numbers allowed are between the minimum and maximum and comma delimited.
Use an
inputMask, aQValidatoror classic RegEx [but dont ask me how to do it with RegEx :) ]Edit:
To get range control (e.g., for an IP address) use masks together with validators.
So you can use both to set your range
-
Use an
inputMask, aQValidatoror classic RegEx [but dont ask me how to do it with RegEx :) ]Edit:
To get range control (e.g., for an IP address) use masks together with validators.
So you can use both to set your range
@Pl45m4 , I'm trying:
QString strRegEx = QString("^[%1-%2](,[%1-%2])*$").arg(strMin, strMax); QRegExp rx(strRegEx); QValidator* pValidator(new QRegExpValidator(rx,this)); plnedInput->setInputMask("0,0"); plnedInput->setValidator(pValidator); QObject::connect(plnedInput, &QLineEdit::editingFinished, [this, pValidator]() { delete pValidator; } );strRegEx for example could contain:
^[1-6](,[1-6])*$However with the validator set it will not accept anything. The requirement is for two numbers comma delimited being within the specified min and max range.
I changed the expression to:
^[1-6],[1-6]$This works when tested on:
http://softlion.com/webTools/RegExpTest/But in Qt I cannot enter anything.
-
@Pl45m4 , I'm trying:
QString strRegEx = QString("^[%1-%2](,[%1-%2])*$").arg(strMin, strMax); QRegExp rx(strRegEx); QValidator* pValidator(new QRegExpValidator(rx,this)); plnedInput->setInputMask("0,0"); plnedInput->setValidator(pValidator); QObject::connect(plnedInput, &QLineEdit::editingFinished, [this, pValidator]() { delete pValidator; } );strRegEx for example could contain:
^[1-6](,[1-6])*$However with the validator set it will not accept anything. The requirement is for two numbers comma delimited being within the specified min and max range.
I changed the expression to:
^[1-6],[1-6]$This works when tested on:
http://softlion.com/webTools/RegExpTest/But in Qt I cannot enter anything.
@SPlatten said in QLineEdit accept comma delimited integers:
(,%1
You forgot a
[plnedInput->setInputMask("0,0");
Remove this, you have a validator
QObject::connect(plnedInput, &QLineEdit::editingFinished, this, pValidator { delete pValidator; } );
Why?! no, the parent will delete it
This only works if
strMinandstrMaxare signle digits -
Hi,
Beside what @VRonin wrote, please use QRegularExpressionValidator. QRegExp was already deprecated in Qt5 and has been removed in Qt6.
Note that the way you do your ranges will only work for single digit values.
-
Hi,
Beside what @VRonin wrote, please use QRegularExpressionValidator. QRegExp was already deprecated in Qt5 and has been removed in Qt6.
Note that the way you do your ranges will only work for single digit values.
-
@SPlatten said in QLineEdit accept comma delimited integers:
(,%1
You forgot a
[plnedInput->setInputMask("0,0");
Remove this, you have a validator
QObject::connect(plnedInput, &QLineEdit::editingFinished, this, pValidator { delete pValidator; } );
Why?! no, the parent will delete it
This only works if
strMinandstrMaxare signle digits@VRonin , yes you are right I did miss the [, however that was only me typing in the source incorrectly here as I couldn't copy and paste from the laptop I am using to code on.
With regard to the comment on the connection to delete the validator, can you elaborate on what you mean by "the parent will delete it" ?
-
Whatever class you use, you'll have to change your regular expression. In fact you'll have to build an expression generator to build your ranges.
-
@VRonin , yes you are right I did miss the [, however that was only me typing in the source incorrectly here as I couldn't copy and paste from the laptop I am using to code on.
With regard to the comment on the connection to delete the validator, can you elaborate on what you mean by "the parent will delete it" ?
@SPlatten said in QLineEdit accept comma delimited integers:
can you elaborate on what you mean by "the parent will delete it" ?
You've set
thisas parent ofpValidator. So it will be cleaned up when the parent (i.e.this) is deleted.
Simple object-tree/ parent-child basics ;-) -
Hi,
Beside what @VRonin wrote, please use QRegularExpressionValidator. QRegExp was already deprecated in Qt5 and has been removed in Qt6.
Note that the way you do your ranges will only work for single digit values.
-
@SPlatten said in QLineEdit accept comma delimited integers:
'QRegularExpressionValidator' to 'const QValidator *'
It's an object but needs a pointer - c++ basics.
-
@SPlatten said in QLineEdit accept comma delimited integers:
'QRegularExpressionValidator' to 'const QValidator *'
It's an object but needs a pointer - c++ basics.
@Christian-Ehrlicher , thank you, sorry its very early here in the UK, brain hasn't woken up yet.