I have a QLineEdit
where the user should input only numbers.
So is there a numbers-only setting for QLineEdit
?
If you're using QT Creator 5.6 you can do that like this:
#include <QIntValidator>
ui->myLineEditName->setValidator( new QIntValidator);
I recomend you put that line after ui->setupUi(this);
I hope this helps.
Why don't you use a QSpinBox
for this purpose ? You can set the up/down buttons invisible with the following line of codes:
// ...
QSpinBox* spinBox = new QSpinBox( this );
spinBox->setButtonSymbols( QAbstractSpinBox::NoButtons ); // After this it looks just like a QLineEdit.
//...
The best is QSpinBox.
And for a double value use QDoubleSpinBox.
QSpinBox myInt;
myInt.setMinimum(-5);
myInt.setMaximum(5);
myInt.setSingleStep(1);// Will increment the current value with 1 (if you use up arrow key) (if you use down arrow key => -1)
myInt.setValue(2);// Default/begining value
myInt.value();// Get the current value
//connect(&myInt, SIGNAL(valueChanged(int)), this, SLOT(myValueChanged(int)));
The Regex Validator
So far, the other answers provide solutions for only a relatively finite number of digits. However, if you're concerned with an arbitrary or a variable number of digits you can use a QRegExpValidator, passing a regex that only accepts digits (as noted by user2962533's comment). Here's a minimal, complete example:
#include <QApplication>
#include <QLineEdit>
#include <QRegExpValidator>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLineEdit le;
le.setValidator(new QRegExpValidator(QRegExp("[0-9]*"), &le));
le.show();
return app.exec();
}
The QRegExpValidator
has its merits (and that's only an understatement). It allows for a bunch of other useful validations:
QRegExp("[1-9][0-9]*") // leading digit must be 1 to 9 (prevents leading zeroes).
QRegExp("\\d*") // allows matching for unicode digits (e.g. for
// Arabic-Indic numerals such as ٤٥٦).
QRegExp("[0-9]+") // input must have at least 1 digit.
QRegExp("[0-9]{8,32}") // input must be between 8 to 32 digits (e.g. for some basic
// password/special-code checks).
QRegExp("[0-1]{,4}") // matches at most four 0s and 1s.
QRegExp("0x[0-9a-fA-F]") // matches a hexadecimal number with one hex digit.
QRegExp("[0-9]{13}") // matches exactly 13 digits (e.g. perhaps for ISBN?).
QRegExp("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}")
// matches a format similar to an ip address.
// N.B. invalid addresses can still be entered: "999.999.999.999".
More On Line-edit Behaviour
According to documentation:
Note that if there is a validator set on the line edit, the returnPressed()/editingFinished() signals will only be emitted if the validator returns QValidator::Acceptable.
Thus, the line-edit will allow the user to input digits even if the minimum amount has not yet been reached. For example, even if the user hasn't inputted any text against the regex "[0-9]{3,}"
(which requires at least 3 digits), the line-edit still allows the user to type input to reach that minimum requirement. However, if the user finishes editing without satsifying the requirement of "at least 3 digits", the input would be invalid; the signals returnPressed()
and editingFinished()
won't be emitted.
If the regex had a maximum-bound (e.g. "[0-1]{,4}"
), then the line-edit will stop any input past 4 characters. Additionally, for character sets (i.e. [0-9]
, [0-1]
, [0-9A-F]
, etc.) the line-edit only allows characters from that particular set to be inputted.
Note that I've only tested this with Qt 5.11 on a macOS, not on other Qt versions or operating systems. But given Qt's cross-platform schema...
Demo: Regex Validators Showcase
QLineEdit::setValidator()
, for example:
myLineEdit->setValidator( new QIntValidator(0, 100, this) );
or
myLineEdit->setValidator( new QDoubleValidator(0, 100, 2, this) );
See: QIntValidator, QDoubleValidator, QLineEdit::setValidator
You could also set an inputMask:
QLineEdit.setInputMask("9")
This allows the user to type only one digit ranging from 0
to 9
. Use multiple 9
's to allow the user to enter multiple numbers. See also the complete list of characters that can be used in an input mask.
(My answer is in Python, but it should not be hard to transform it to C++)