How to restrict QLineEdit to take "comma" and "plus" as input value?



  • Hi,,

    how can I avoid entering comma (,) and plus (+) to QLineEdit field in my Qt ui application??
    and also QSpinBox to not to take + (plus)??



  • hi @kishore_hemmady ,
    see void QLineEdit::setValidator(const QValidator *v) http://doc.qt.io/qt-5/qlineedit.html#setValidator

    QRegExp exp = QRegExp("your regExp");
    QRegExpValidator v = QRegExpValidator(exp,this);
     yourLineEdit.setValidator(v);
    


  • thank you @LeLev

    please tell me how can i set range for this field using the below snippet?

    QRegExp rx("-?\\d{1,3}");
    QValidator *validator = new QRegExpValidator(rx, this);
    ui->VideoHeight->setValidator(validator);
    

    I want to set the range between 1 to 100.



  • @kishore_hemmady Hi,

    Better to use QIntValidator in this case:

    QValidator *validator = new QIntValidator(1, 100, this);
    // the edit lineedit will only accept integers between 1 and 100 
    edit->setValidator(validator);
    


  • hi @Gojir4 ,

    thank you,
    i had used the same but ,i was able to enter comma (,)and plus (+) symbol if i use the above code,which i don't want.



  • Hello,

    You could install an eventFilter on your lineEdit and trap the comma, '+' and '-' sign and use the int validator as well.

    Regards


  • Lifetime Qt Champion

    Hi,

    What is your widget purpose ?



  • @kishore_hemmady

    You can create a RegularExpression to accept just 0 or 1-99 or 100

    QRegExp regExp = QRegExp("^(0|[1-9][0-9]?|100)$");
    

    | = OR
    ? = OPTIONAL

    NOTE: if you are using Qt5.*, i suggest you to use QRegularExpression instead of QRegExp, because QRegExp is an old class of regular expression of Qt4 :)
    http://doc.qt.io/qt-5/qregularexpression.html#notes-for-qregexp-users



  • Hi @SGaist

    its a variable , i have to set it like this,
    ui->height->setValidator(newQIntValidator(minValue,maxValue, this));
    if i use this then the height field will take + and , values.

    minValue and maxValue values are system dependent


  • Lifetime Qt Champion

    If you still have a comma that you can enter then there's something else that's wrong neither QIntValidator nor QSpinBox allows that kind of number.

    What is the problem with the plus sign ?



  • @kishore_hemmady
    If you really need to avoid the + signal and just accept numbers.

    1) You can create a custom QIntValidator and reimplement the validate function to accept what you need.

    // customintvalidator.h
    #include <QIntValidator>
    
    class CustomIntValidator : public QIntValidator {
    public:
        CustomIntValidator (int bottom, int top, QObject * parent) : QIntValidator(bottom, top, parent) {}
    
        QValidator::State validate(QString &s, int &pos) const {
            Q_UNUSED(pos);
            if (s.isEmpty()){
                return QValidator::Intermediate; // accept but is not valid
            }
    
            if( s.startsWith("+") ){
                return QValidator::Invalid;
            }
    
            if( bottom() < 0 && s.startsWith("-") && s.length() == 1 ){
                return QValidator::Intermediate;
            }
    
            // check number
    
            bool isNumber;
            int value = locale().toInt(s, &isNumber);
    
            if (isNumber && bottom() <= value && value <= top()) {
                return QValidator::Acceptable;
            }
    
            return QValidator::Invalid;
        }
    
    };
    
    // mainwindow.cpp
    ui->lineEdit->setValidator(new CustomIntValidator(-100 , 100, this));
    

    This custom IntValidator accepts positive and negative numbers incluinding -0 interpreted as 0
    And ignore comma, dot and plus simbols.

    2) You can create a filter to capture the Comma and Plus Keydown event

    // filterobject.h
    #include <QEvent>
    #include <QKeyEvent>
    #include <QObject>
    
    class FilterObject : public QObject {
    public:
        explicit FilterObject(QObject *parent = 0) : QObject(parent){
        }
        
        bool eventFilter(QObject *object, QEvent *event)
        {
            Q_UNUSED(object);
            if (event->type() == QEvent::KeyPress) {
                QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                switch(keyEvent->key()){
                case Qt::Key_Plus:
                    return true;
                case Qt::Key_Comma:
                    return true;
                default:
                    return false;
                }
            }
            return false;
        }
    };
    
    // mainwindow.cpp
    ui->lineEdit->installEventFilter(new FilterObject(ui->lineEdit));
    

    I don't believe these are the best solutions for this problem but works.



  • hi @KillerSmath
    thank you for your answer.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.