태그 보관물: qt

qt

숫자 만 허용하도록 QLineEdit 설정 숫자 만. 숫자 전용 설정이 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)));


답변

정규식 검사기

지금까지 다른 답변은 상대적으로 유한 한 자릿수에 대한 솔루션을 제공합니다 . 그러나 임의의 또는 가변적 인 자릿수에 관심이있는 경우 숫자 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".     

더 많은 온라인 편집 동작

문서 에 따르면 :

라인 편집에 유효성 검사기가 설정되어 있으면 유효성 검사기가 QValidator :: Acceptable을 반환하는 경우에만 returnPressed () / editingFinished () 신호가 방출됩니다.

따라서 라인 편집을 통해 사용자는 최소 금액에 도달하지 않았더라도 숫자를 입력 할 수 있습니다. 예를 들어 사용자가 정규식 "[0-9]{3,}"(최소 3 자리 필요)에 대해 텍스트를 입력하지 않은 경우에도 줄 편집을 통해 사용자 는 최소 요구 사항 에 도달 하기 위해 입력을 입력 할 수 있습니다 . 그러나 사용자가 “최소 3 자리”요구 사항을 충족하지 않고 편집을 마치면 입력이 유효하지 않습니다 . 신호 returnPressed()및이 editingFinished()방출되지 않는다.

정규식에 최대 제한 (예 :)이있는 경우 "[0-1]{,4}"라인 편집은 4자를 초과하는 모든 입력을 중지합니다. 또한, 문자 집합 (예를 위해 [0-9], [0-1], [0-9A-F], 등) 라인 편집 만의 문자 수 있습니다 특정 세트를 입력 할 수 있습니다.

다른 Qt 버전이나 운영 체제가 아닌 macOS의 Qt 5.11에서만 이것을 테스트했습니다. 그러나 Qt의 크로스 플랫폼 스키마가 주어지면 …

데모 : Regex Validators Showcase


답변

다음을 설정할 수도 있습니다 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.
//...


답변

QT Creator 5.6을 사용하는 경우 다음과 같이 할 수 있습니다.

#include <QIntValidator>

ui->myLineEditName->setValidator( new QIntValidator);

ui-> setupUi (this); 다음에 그 줄을 넣는 것이 좋습니다.

이게 도움이 되길 바란다.


답변