숫자 만 허용하도록 QLineEdit 설정
나는이 QLineEdit경우 사용자가 입력해야 숫자 만.
숫자 전용 설정이 QLineEdit있습니까?
QLineEdit::setValidator()예 :
myLineEdit->setValidator( new QIntValidator(0, 100, this) );
또는
myLineEdit->setValidator( new QDoubleValidator(0, 100, 2, this) );
참조 : QIntValidator , QDoubleValidator , QLineEdit :: setValidator
최고는 QSpinBox입니다.
그리고 이중 값을 사용하려면 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)));
다음을 설정할 수도 있습니다 inputMask.
QLineEdit.setInputMask("9")
이것은 사용자에 이르기까지 한 자릿수 만 입력 할 수 있습니다 0에를 9. 9사용자가 여러 번호를 입력 할 수 있도록 여러 개의를 사용하십시오 . 입력 마스크에 사용할 수있는 전체 문자 목록을 참조하십시오 .
(내 대답은 Python이지만 C ++로 변환하는 것은 어렵지 않습니다)
QSpinBox이 목적으로 a 를 사용하지 않는 이유는 무엇 입니까? 다음 코드 줄을 사용하여 보이지 않는 위 / 아래 버튼을 설정할 수 있습니다.
// ...
QSpinBox* spinBox = new QSpinBox( this );
spinBox->setButtonSymbols( QAbstractSpinBox::NoButtons ); // After this it looks just like a QLineEdit.
//...
정규식 검사기
지금까지 다른 답변은 상대적으로 유한 한 자릿수에 대한 솔루션을 제공합니다 . 그러나 임의의 또는 가변적 인 자릿수에 관심이있는 경우 숫자 QRegExpValidator만 허용하는 정규식을 전달하는을 사용할 수 있습니다 ( user2962533의 주석에서 언급 한대로 ). 다음은 최소한의 완전한 예입니다.
#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();
}
는 QRegExpValidator그 장점을 가지고 (그에만 약하게이다). 다른 유용한 유효성 검사가 가능합니다.
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".
더 많은 온라인 편집 동작
문서 에 따르면 :
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
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.
참고URL : https://stackoverflow.com/questions/13422995/set-qlineedit-to-accept-only-numbers
'Programing' 카테고리의 다른 글
| Java에서 클래스가 Serializable을 올바르게 구현하는지 테스트하는 방법 (단지 Serializable의 인스턴스가 아님) (0) | 2020.11.01 |
|---|---|
| 리눅스 정규식 찾기 (0) | 2020.11.01 |
| 신속하게 터치하거나 클릭 한 tableView 셀을 감지하는 방법 (0) | 2020.11.01 |
| 다른 네임 스페이스에있는 서비스 (0) | 2020.11.01 |
| libtool 버전 불일치 오류 (0) | 2020.11.01 |